Skip to content

Commit b9a78ac

Browse files
authored
refactor(core): allow opt-out ctor dependency (#7196)
* refactor(core): allow opt-out ctor dependency Signed-off-by: tison <wander4096@gmail.com> * fixup Signed-off-by: tison <wander4096@gmail.com> * rename feature Signed-off-by: tison <wander4096@gmail.com> --------- Signed-off-by: tison <wander4096@gmail.com>
1 parent 9d3e68b commit b9a78ac

File tree

6 files changed

+30
-35
lines changed

6 files changed

+30
-35
lines changed

core/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ unused_async = "warn"
8181
all-features = true
8282

8383
[features]
84+
# Register services enabled to the OperatorRegistry so that they can be used by `Operator::from_uri`.
85+
auto-register-services = ["dep:ctor"]
8486
blocking = ["opendal-core/blocking"]
8587
default = [
88+
"auto-register-services",
8689
"reqwest-rustls-tls",
8790
"executors-tokio",
8891
"layers-concurrent-limit",
@@ -207,7 +210,7 @@ path = "tests/behavior/main.rs"
207210
required-features = ["tests"]
208211

209212
[dependencies]
210-
ctor = { workspace = true }
213+
ctor = { workspace = true, optional = true }
211214
opendal-core = { path = "core", version = "0.55.0", default-features = false }
212215
opendal-layer-async-backtrace = { path = "layers/async-backtrace", version = "0.55.0", optional = true, default-features = false }
213216
opendal-layer-await-tree = { path = "layers/await-tree", version = "0.55.0", optional = true, default-features = false }

core/core/src/types/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ mod execute;
4343
pub use execute::*;
4444

4545
mod operator;
46-
pub use operator::DEFAULT_OPERATOR_REGISTRY;
4746
pub use operator::IntoOperatorUri;
4847
pub use operator::Operator;
4948
pub use operator::OperatorBuilder;

core/core/src/types/operator/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Operator {
142142
/// # }
143143
/// ```
144144
pub fn from_uri(uri: impl IntoOperatorUri) -> Result<Operator> {
145-
crate::DEFAULT_OPERATOR_REGISTRY.load(uri)
145+
OperatorRegistry::get().load(uri)
146146
}
147147

148148
/// Create a new operator via given scheme and iterator of config value in dynamic dispatch.

core/core/src/types/operator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use info::OperatorInfo;
3030
pub mod operator_futures;
3131

3232
mod registry;
33-
pub use registry::{DEFAULT_OPERATOR_REGISTRY, OperatorFactory, OperatorRegistry};
33+
pub use registry::{OperatorFactory, OperatorRegistry};
3434

3535
mod uri;
3636
pub use uri::{IntoOperatorUri, OperatorUri};

core/core/src/types/operator/registry.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,38 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::collections::HashMap;
19-
use std::sync::{LazyLock, Mutex};
20-
2118
use crate::types::builder::{Builder, Configurator};
2219
use crate::types::{IntoOperatorUri, OperatorUri};
2320
use crate::{Error, ErrorKind, Operator, Result};
21+
use std::collections::HashMap;
22+
use std::sync::{LazyLock, Mutex};
2423

2524
/// Factory signature used to construct [`Operator`] from a URI and extra options.
2625
pub type OperatorFactory = fn(&OperatorUri) -> Result<Operator>;
2726

28-
/// Default registry used by [`Operator::from_uri`].
29-
///
30-
/// `memory` is always registered here since it's used pervasively in unit tests
31-
/// and as a zero-dependency backend.
32-
///
33-
/// Other optional service registrations are handled by the facade crate `opendal`.
34-
pub static DEFAULT_OPERATOR_REGISTRY: LazyLock<OperatorRegistry> = LazyLock::new(|| {
35-
let registry = OperatorRegistry::new();
36-
37-
crate::services::register_memory_service(&registry);
38-
39-
registry
40-
});
41-
4227
/// Global registry that maps schemes to [`OperatorFactory`] functions.
43-
#[derive(Debug, Default)]
28+
#[derive(Debug)]
4429
pub struct OperatorRegistry {
4530
factories: Mutex<HashMap<String, OperatorFactory>>,
4631
}
4732

4833
impl OperatorRegistry {
49-
/// Create a new, empty registry.
50-
pub fn new() -> Self {
51-
Self {
52-
factories: Mutex::new(HashMap::new()),
53-
}
34+
/// Get the global registry.
35+
pub fn get() -> &'static Self {
36+
/// The global registry used by [`Operator::from_uri`].
37+
///
38+
/// `memory` is always registered here since it's used pervasively in unit tests
39+
/// and as a zero-dependency backend.
40+
///
41+
/// Other optional service registrations are handled by the facade crate `opendal`.
42+
static OPERATOR_REGISTRY: LazyLock<OperatorRegistry> = LazyLock::new(|| {
43+
let factories = Mutex::new(HashMap::default());
44+
let registry = OperatorRegistry { factories };
45+
crate::services::register_memory_service(&registry);
46+
registry
47+
});
48+
49+
&OPERATOR_REGISTRY
5450
}
5551

5652
/// Register a builder for the given scheme.

core/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ pub use opendal_core::*;
2727
#[cfg(feature = "tests")]
2828
pub extern crate opendal_testkit as tests;
2929

30-
static DEFAULT_REGISTRY_INIT: std::sync::Once = std::sync::Once::new();
31-
32-
/// Initialize [`DEFAULT_OPERATOR_REGISTRY`] with enabled services.
30+
/// Initialize the global [`OperatorRegistry`] with enabled services.
3331
///
3432
/// This function is safe to call multiple times and will only perform
3533
/// initialization once.
@@ -41,13 +39,11 @@ static DEFAULT_REGISTRY_INIT: std::sync::Once = std::sync::Once::new();
4139
/// should call this function explicitly before using `Operator::from_uri` or
4240
/// `Operator::via_iter`.
4341
pub fn init_default_registry() {
44-
DEFAULT_REGISTRY_INIT.call_once(|| {
45-
let registry = &opendal_core::DEFAULT_OPERATOR_REGISTRY;
46-
init_default_registry_inner(registry);
47-
});
42+
static DEFAULT_REGISTRY_INIT: std::sync::Once = std::sync::Once::new();
43+
DEFAULT_REGISTRY_INIT.call_once(|| init_default_registry_inner(OperatorRegistry::get()));
4844
}
4945

50-
fn init_default_registry_inner(registry: &opendal_core::OperatorRegistry) {
46+
fn init_default_registry_inner(registry: &OperatorRegistry) {
5147
opendal_core::services::register_memory_service(registry);
5248

5349
#[cfg(feature = "services-aliyun-drive")]
@@ -234,6 +230,7 @@ fn init_default_registry_inner(registry: &opendal_core::OperatorRegistry) {
234230
opendal_service_redis::register_redis_service(registry);
235231
}
236232

233+
#[cfg(feature = "auto-register-services")]
237234
#[ctor::ctor]
238235
fn register_default_operator_registry() {
239236
init_default_registry();

0 commit comments

Comments
 (0)