From 65486b185ab725878f34fa6f16d52f623a26bf34 Mon Sep 17 00:00:00 2001 From: Christian Grigis <christian.grigis@epfl.ch> Date: Tue, 1 Mar 2022 10:46:30 +0100 Subject: [PATCH] Expose APIs for external DirProvider --- crates/tor-dirmgr/Cargo.toml | 6 ++++++ crates/tor-dirmgr/src/config.rs | 12 ++++++------ crates/tor-dirmgr/src/lib.rs | 5 ++++- crates/tor-dirmgr/src/shared_ref.rs | 12 +++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/tor-dirmgr/Cargo.toml b/crates/tor-dirmgr/Cargo.toml index 70323b0fb8..e195b0d421 100644 --- a/crates/tor-dirmgr/Cargo.toml +++ b/crates/tor-dirmgr/Cargo.toml @@ -17,6 +17,12 @@ static = ["rusqlite/bundled"] # (Incomplete) support for downloading and storing router descriptors routerdesc = ["tor-dirclient/routerdesc"] +# Enable experimental APIs that are not yet officially supported. +# +# These APIs are not covered by semantic versioning. Using this +# feature voids your "semver warrantee". +experimental-api = [] + [dependencies] tor-basic-utils = { path="../tor-basic-utils", version = "0.1.0"} retry-error = { path = "../retry-error", version = "0.1.0"} diff --git a/crates/tor-dirmgr/src/config.rs b/crates/tor-dirmgr/src/config.rs index 536d6bf4d8..7e889d60f9 100644 --- a/crates/tor-dirmgr/src/config.rs +++ b/crates/tor-dirmgr/src/config.rs @@ -257,28 +257,28 @@ impl DirMgrConfig { } /// Return the configured cache path. - pub(crate) fn cache_path(&self) -> &std::path::Path { + pub fn cache_path(&self) -> &std::path::Path { self.cache_path.as_ref() } /// Return a slice of the configured authorities - pub(crate) fn authorities(&self) -> &[Authority] { + pub fn authorities(&self) -> &[Authority] { self.network_config.authorities() } /// Return the configured set of fallback directories - pub(crate) fn fallbacks(&self) -> &[FallbackDir] { + pub fn fallbacks(&self) -> &[FallbackDir] { self.network_config.fallbacks() } /// Return set of configured networkstatus parameter overrides. - pub(crate) fn override_net_params(&self) -> &netstatus::NetParams<i32> { + pub fn override_net_params(&self) -> &netstatus::NetParams<i32> { &self.override_net_params } /// Return the schedule configuration we should use to decide when to /// attempt and retry downloads. - pub(crate) fn schedule(&self) -> &DownloadScheduleConfig { + pub fn schedule(&self) -> &DownloadScheduleConfig { &self.schedule_config } @@ -286,7 +286,7 @@ impl DirMgrConfig { /// `self` are replaced with those from `new_config`. /// /// Any fields which aren't allowed to change at runtime are copied from self. - pub(crate) fn update_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig { + pub fn update_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig { DirMgrConfig { cache_path: self.cache_path.clone(), network_config: NetworkConfig { diff --git a/crates/tor-dirmgr/src/lib.rs b/crates/tor-dirmgr/src/lib.rs index 926430e31c..a0d6f5c47c 100644 --- a/crates/tor-dirmgr/src/lib.rs +++ b/crates/tor-dirmgr/src/lib.rs @@ -56,7 +56,7 @@ pub mod authority; mod bootstrap; -mod config; +pub mod config; mod docid; mod docmeta; mod err; @@ -67,7 +67,10 @@ mod state; mod storage; use crate::docid::{CacheUsage, ClientRequest, DocQuery}; +#[cfg(not(feature = "experimental-api"))] use crate::shared_ref::SharedMutArc; +#[cfg(feature = "experimental-api")] +pub use crate::shared_ref::SharedMutArc; use crate::storage::DynStore; use postage::watch; pub use retry::DownloadSchedule; diff --git a/crates/tor-dirmgr/src/shared_ref.rs b/crates/tor-dirmgr/src/shared_ref.rs index 38630b6e84..d1b83b9b29 100644 --- a/crates/tor-dirmgr/src/shared_ref.rs +++ b/crates/tor-dirmgr/src/shared_ref.rs @@ -16,7 +16,8 @@ use crate::{Error, Result}; // sure we don't hold the lock against any async suspend points. #[derive(Debug, Educe)] #[educe(Default)] -pub(crate) struct SharedMutArc<T> { +#[cfg_attr(not(feature = "experimental-api"), allow(unreachable_pub))] +pub struct SharedMutArc<T> { /// Locked reference to the current value. /// /// (It's okay to use RwLock here, because we never suspend @@ -24,14 +25,15 @@ pub(crate) struct SharedMutArc<T> { dir: RwLock<Option<Arc<T>>>, } +#[cfg_attr(not(feature = "experimental-api"), allow(unreachable_pub))] impl<T> SharedMutArc<T> { /// Construct a new empty SharedMutArc. - pub(crate) fn new() -> Self { + pub fn new() -> Self { SharedMutArc::default() } /// Replace the current value with `new_val`. - pub(crate) fn replace(&self, new_val: T) { + pub fn replace(&self, new_val: T) { let mut w = self .dir .write() @@ -50,7 +52,7 @@ impl<T> SharedMutArc<T> { } /// Return a new reference to the current value, if there is one. - pub(crate) fn get(&self) -> Option<Arc<T>> { + pub fn get(&self) -> Option<Arc<T>> { let r = self .dir .read() @@ -72,7 +74,7 @@ impl<T> SharedMutArc<T> { /// and future attempts to use it will panic. (TODO: Fix this.) // Note: If we decide to make this type public, we'll probably // want to fiddle with how we handle the return type. - pub(crate) fn mutate<F, U>(&self, func: F) -> Result<U> + pub fn mutate<F, U>(&self, func: F) -> Result<U> where F: FnOnce(&mut T) -> Result<U>, T: Clone, -- GitLab