diff options
author | HampusM <hampus@hampusmat.com> | 2023-07-31 20:52:37 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-07-31 20:52:37 +0200 |
commit | 343661391c21f535e7b832f3fef05e09a61a0a29 (patch) | |
tree | e6b06b2fa2addab0a1aa5ae209e5b8b9cf91cfe4 /src | |
parent | 118651055b7844e04aa5b89023c385e483de305f (diff) |
refactor!: remove SomeThreadsafePtrError
BREAKING CHANGE: SomeThreadsafePtrError has been removed and SomePtrError is now used by both the methods of SomePtr and of SomeThreadsafePtr
Diffstat (limited to 'src')
-rw-r--r-- | src/errors/ptr.rs | 21 | ||||
-rw-r--r-- | src/ptr.rs | 22 |
2 files changed, 14 insertions, 29 deletions
diff --git a/src/errors/ptr.rs b/src/errors/ptr.rs index 56621c1..1db10c7 100644 --- a/src/errors/ptr.rs +++ b/src/errors/ptr.rs @@ -1,8 +1,9 @@ //! Smart pointer alias errors. -/// Error type for [`SomePtr`]. +/// Error type for [`SomePtr`] and [`SomeThreadsafePtr`]. /// /// [`SomePtr`]: crate::ptr::SomePtr +/// [`SomeThreadsafePtr`]: crate::ptr::SomeThreadsafePtr #[derive(thiserror::Error, Debug)] pub enum SomePtrError { @@ -17,21 +18,3 @@ pub enum SomePtrError found: &'static str, }, } - -/// Error type for [`SomeThreadsafePtr`]. -/// -/// [`SomeThreadsafePtr`]: crate::ptr::SomeThreadsafePtr -#[derive(thiserror::Error, Debug)] -pub enum SomeThreadsafePtrError -{ - /// Tried to get as a wrong threadsafe smart pointer type. - #[error("Wrong threadsafe smart pointer type. Expected {expected}, found {found}")] - WrongPtrType - { - /// The expected smart pointer type. - expected: &'static str, - - /// The found smart pointer type. - found: &'static str, - }, -} @@ -4,7 +4,7 @@ use std::sync::Arc; use paste::paste; -use crate::errors::ptr::{SomePtrError, SomeThreadsafePtrError}; +use crate::errors::ptr::SomePtrError; /// A smart pointer for a interface in the transient scope. pub type TransientPtr<Interface> = Box<Interface>; @@ -26,11 +26,11 @@ pub type FactoryPtr<FactoryInterface> = Rc<FactoryInterface>; pub type ThreadsafeFactoryPtr<FactoryInterface> = Arc<FactoryInterface>; macro_rules! create_as_variant_fn { - ($enum: ident, $variant: ident) => { - create_as_variant_fn!($enum, $variant,); + ($enum: ident, $variant: ident, $err: ident) => { + create_as_variant_fn!($enum, $variant, $err,); }; - ($enum: ident, $variant: ident, $($attrs: meta),*) => { + ($enum: ident, $variant: ident, $err: ident, $($attrs: meta),*) => { paste! { #[doc = "Returns as the `" [<$variant>] "` variant.\n" @@ -39,13 +39,13 @@ macro_rules! create_as_variant_fn { "Will return Err if it's not the `" [<$variant>] "` variant." ] $(#[$attrs])* - pub fn [<$variant:snake>](self) -> Result<[<$variant Ptr>]<Interface>, [<$enum Error>]> + pub fn [<$variant:snake>](self) -> Result<[<$variant Ptr>]<Interface>, $err> { if let $enum::$variant(ptr) = self { return Ok(ptr); } - Err([<$enum Error>]::WrongPtrType { + Err($err::WrongPtrType { expected: stringify!($variant), found: self.into() }) @@ -76,13 +76,14 @@ impl<Interface> SomePtr<Interface> where Interface: 'static + ?Sized, { - create_as_variant_fn!(SomePtr, Transient); + create_as_variant_fn!(SomePtr, Transient, SomePtrError); - create_as_variant_fn!(SomePtr, Singleton); + create_as_variant_fn!(SomePtr, Singleton, SomePtrError); create_as_variant_fn!( SomePtr, Factory, + SomePtrError, cfg(feature = "factory"), cfg_attr(doc_cfg, doc(cfg(feature = "factory"))) ); @@ -110,13 +111,14 @@ impl<Interface> SomeThreadsafePtr<Interface> where Interface: 'static + ?Sized, { - create_as_variant_fn!(SomeThreadsafePtr, Transient); + create_as_variant_fn!(SomeThreadsafePtr, Transient, SomePtrError); - create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeSingleton); + create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeSingleton, SomePtrError); create_as_variant_fn!( SomeThreadsafePtr, ThreadsafeFactory, + SomePtrError, cfg(feature = "factory"), cfg_attr(doc_cfg, doc(cfg(feature = "factory"))) ); |