diff --git a/src/fn_native.rs b/src/fn_native.rs index 11801050..9296da62 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -18,11 +18,6 @@ use crate::{ Position, RhaiResult, }; -#[cfg(not(feature = "sync"))] -use crate::stdlib::rc::Rc; -#[cfg(feature = "sync")] -use crate::stdlib::sync::Arc; - /// Trait that maps to `Send + Sync` only under the `sync` feature. #[cfg(feature = "sync")] pub trait SendSync: Send + Sync {} @@ -39,19 +34,19 @@ impl SendSync for T {} /// Immutable reference-counted container. #[cfg(not(feature = "sync"))] -pub type Shared = Rc; +pub use crate::stdlib::rc::Rc as Shared; /// Immutable reference-counted container. #[cfg(feature = "sync")] -pub type Shared = Arc; +pub use crate::stdlib::sync::Arc as Shared; /// Synchronized shared object. #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] -pub type Locked = crate::stdlib::cell::RefCell; +pub use crate::stdlib::cell::RefCell as Locked; /// Synchronized shared object. #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] -pub type Locked = crate::stdlib::sync::RwLock; +pub use crate::stdlib::sync::RwLock as Locked; /// Context of a native Rust function call. #[derive(Debug, Copy, Clone)] @@ -215,10 +210,7 @@ impl<'a> NativeCallContext<'a> { /// If the resource is shared (i.e. has other outstanding references), a cloned copy is used. #[inline(always)] pub fn shared_make_mut(value: &mut Shared) -> &mut T { - #[cfg(not(feature = "sync"))] - return Rc::make_mut(value); - #[cfg(feature = "sync")] - return Arc::make_mut(value); + Shared::make_mut(value) } /// Consume a [`Shared`] resource if is unique (i.e. not shared), or clone it otherwise. @@ -230,10 +222,7 @@ pub fn shared_take_or_clone(value: Shared) -> T { /// Consume a [`Shared`] resource if is unique (i.e. not shared). #[inline(always)] pub fn shared_try_take(value: Shared) -> Result> { - #[cfg(not(feature = "sync"))] - return Rc::try_unwrap(value); - #[cfg(feature = "sync")] - return Arc::try_unwrap(value); + Shared::try_unwrap(value) } /// Consume a [`Shared`] resource, assuming that it is unique (i.e. not shared). diff --git a/src/lib.rs b/src/lib.rs index 72a8c881..94652bed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,7 +125,7 @@ pub type FLOAT = f32; pub use ast::{FnAccess, ScriptFnMetadata, AST}; pub use dynamic::Dynamic; pub use engine::{Engine, EvalContext}; -pub use fn_native::{FnPtr, NativeCallContext, Shared}; +pub use fn_native::{FnPtr, NativeCallContext}; pub use fn_register::{RegisterFn, RegisterResultFn}; pub use module::{FnNamespace, Module}; pub use parse_error::{LexError, ParseError, ParseErrorType}; @@ -135,6 +135,9 @@ pub use syntax::Expression; pub use token::Position; pub use utils::ImmutableString; +/// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag. +pub use fn_native::Shared; + #[cfg(not(feature = "no_closure"))] use fn_native::Locked;