From 343661391c21f535e7b832f3fef05e09a61a0a29 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 31 Jul 2023 20:52:37 +0200 Subject: refactor!: remove SomeThreadsafePtrError BREAKING CHANGE: SomeThreadsafePtrError has been removed and SomePtrError is now used by both the methods of SomePtr and of SomeThreadsafePtr --- src/errors/ptr.rs | 21 ++------------------- 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, - }, -} diff --git a/src/ptr.rs b/src/ptr.rs index 46418d1..bcaa566 100644 --- a/src/ptr.rs +++ b/src/ptr.rs @@ -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 = Box; @@ -26,11 +26,11 @@ pub type FactoryPtr = Rc; pub type ThreadsafeFactoryPtr = Arc; 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>], [<$enum Error>]> + pub fn [<$variant:snake>](self) -> Result<[<$variant Ptr>], $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 SomePtr 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 SomeThreadsafePtr 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"))) ); -- cgit v1.2.3-18-g5258