diff --git a/module/core/format_tools/Cargo.toml b/module/core/format_tools/Cargo.toml index 188cb9b194..e299dd0490 100644 --- a/module/core/format_tools/Cargo.toml +++ b/module/core/format_tools/Cargo.toml @@ -40,7 +40,7 @@ enabled = [ [dependencies] # derive_tools = { workspace = true, features = [ "derive_from", "derive_inner_from" ] } -reflect_tools = { workspace = true, features = [ "reflect_reflect" ] } +reflect_tools = { workspace = true, features = [ "reflect_types" ] } former = { workspace = true, features = [ "full" ] } # qqq : xxx : optmize set of features diff --git a/module/core/format_tools/src/format/to_string.rs b/module/core/format_tools/src/format/to_string.rs index a486c62be2..9785644eaa 100644 --- a/module/core/format_tools/src/format/to_string.rs +++ b/module/core/format_tools/src/format/to_string.rs @@ -6,9 +6,15 @@ pub( crate ) mod private { + pub use super:: + { + aref::{ Ref, _Ref }, + }; + use std:: { fmt, + borrow::Cow, }; // == @@ -28,45 +34,153 @@ pub( crate ) mod private // == /// Trait to convert a type to a string using a specified formatting method. - pub trait ToStringWith< How > + pub trait ToStringWith< 'a, How > { /// Converts the type to a string using the specified formatting method. - fn to_string_with( &self ) -> String; + fn to_string_with( &'a self ) -> Cow< 'a, str >; } - impl< T > ToStringWith< WithDebug > for T + impl< 'a, T > ToStringWith< 'a, WithDebug > for T where T : fmt::Debug, { /// Converts the type to a string using Debug formatting. - fn to_string_with( &self ) -> String + #[ inline ] + fn to_string_with( &'a self ) -> Cow< 'a, str > { - format!( "{:?}", self ) + Cow::Owned( format!( "{:?}", self ) ) } } - impl< T > ToStringWith< WithDisplay > for T + // impl< 'a, T > ToStringWith< 'a, WithDebug > for &T + // where + // T : fmt::Debug, + // { + // /// Converts the type to a string using Debug formatting. + // #[ inline ] + // fn to_string_with( &'a self ) -> Cow< 'a, str > + // { + // ToStringWith::< 'a, WithDebug >::to_string_with( *self ) + // } + // } + + impl< 'a, T > ToStringWith< 'a, WithDisplay > for T where T : fmt::Display, { /// Converts the type to a string using Display formatting. - fn to_string_with( &self ) -> String + #[ inline ] + fn to_string_with( &'a self ) -> Cow< 'a, str > { - format!( "{}", self ) + ( &Ref::< 'a, T, WithDisplay >::from( self ) )._display_string() + // Cow::Owned( format!( "{}", self ) ) } } - // impl ToStringWith< WithDisplay > for String - // { - // /// Converts the type to a string using Display formatting. - // fn to_string_with( &self ) -> String - // { - // format!( "x{}", self ) - // } - // } + trait _DisplayString< 'a > + { + fn _display_string( self ) -> Cow< 'a, str >; + } + + impl< 'a, T > _DisplayString< 'a > for _Ref< 'a, T, WithDisplay > + where + T : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + // panic!( "a" ); + Cow::Owned( format!( "{}", self.0 ) ) + } + } + + // xxx : not only String + + impl< 'a > _DisplayString< 'a > for Ref< 'a, String, WithDisplay > + where + String : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + panic!( "xxx" ); + Cow::Borrowed( self.0.0 ) + } + } + + impl< 'a > _DisplayString< 'a > for Ref< 'a, &String, WithDisplay > + where + String : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + panic!( "yyy" ); + Cow::Borrowed( self.0.0 ) + } + } + + impl< 'a > _DisplayString< 'a > for Ref< 'a, &&String, WithDisplay > + where + String : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + panic!( "yyy" ); + Cow::Borrowed( self.0.0 ) + } + } + + impl< 'a > _DisplayString< 'a > for Ref< 'a, str, WithDisplay > + where + str : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + panic!( "zzz1" ); + Cow::Borrowed( self.0.0 ) + } + } + + impl< 'a > _DisplayString< 'a > for Ref< 'a, &str, WithDisplay > + where + str : fmt::Display, + { + #[ inline ] + fn _display_string( self ) -> Cow< 'a, str > + { + panic!( "zzz2" ); + Cow::Borrowed( self.0.0 ) + } + } + + // xxx + fn _f< 'a, T : 'a >() + where + Ref::< 'a, T, WithDisplay > : _DisplayString< 'a >, + { + } + + // xxx : clean + +// #[ test ] +// fn borrowed_string_test() +// { +// +// let src = "string".to_string(); +// let got = ToStringWith::< WithDisplay >::to_string_with( &src ); +// let exp : Cow< '_, str > = Cow::Borrowed( "string" ); +// assert_eq!( got, exp ); +// assert!( matches!( got, Cow::Borrowed( _ ) ) ); +// +// } } +mod aref; + #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use own::*; @@ -78,6 +192,11 @@ pub mod own use super::*; #[ doc( inline ) ] pub use orphan::*; + #[ doc( inline ) ] + pub use private:: + { + Ref, + }; } /// Orphan namespace of the module. @@ -87,13 +206,7 @@ pub mod orphan use super::*; #[ doc( inline ) ] pub use exposed::*; -} -/// Exposed namespace of the module. -#[ allow( unused_imports ) ] -pub mod exposed -{ - use super::*; #[ doc( inline ) ] pub use private:: { @@ -102,6 +215,16 @@ pub mod exposed WithWell, ToStringWith, }; + +} + +/// Exposed namespace of the module. +#[ allow( unused_imports ) ] +pub mod exposed +{ + use super::*; + #[ doc( inline ) ] + pub use prelude::*; } /// Prelude to use essentials: `use my_module::prelude::*`. diff --git a/module/core/format_tools/src/format/to_string/aref.rs b/module/core/format_tools/src/format/to_string/aref.rs new file mode 100644 index 0000000000..32fe2f30bc --- /dev/null +++ b/module/core/format_tools/src/format/to_string/aref.rs @@ -0,0 +1,93 @@ +//! +//! Wrapper to wrap argument for trait `ToStringWith`. +//! + +// use core::fmt; +use core::ops::{ Deref }; + +/// Reference wrapper to make into string conversion with fallback. +#[ allow( missing_debug_implementations ) ] +#[ repr( transparent ) ] +pub struct Ref< 'a, T, How > +( pub _Ref< 'a, T, How > ) +where + &'a T : Copy, + T : ?Sized, +; + +/// Internal reference wrapper to make into string conversion with fallback. +#[ allow( missing_debug_implementations ) ] +#[ repr( transparent ) ] +pub struct _Ref< 'a, T, How > +( pub &'a T, ::core::marker::PhantomData< fn() -> How > ) +where + ::core::marker::PhantomData< fn() -> How > : Copy, + &'a T : Copy, + T : ?Sized, +; + +impl< 'a, T, How > Ref< 'a, T, How > +{ + + // /// Just a constructor. + // #[ inline( always ) ] + // pub fn new( src : &'a T ) -> Self + // { + // Self( src, ::core::marker::PhantomData ) + // } + + /// Just a constructor. + #[ inline( always ) ] + pub fn inner( self ) -> &'a T + { + self.0.0 + } + +} + +impl< 'a, T, How > Clone for Ref< 'a, T, How > +{ + #[ inline( always ) ] + fn clone( &self ) -> Self + { + Self( self.0 ) + } +} + +impl< 'a, T, How > Clone for _Ref< 'a, T, How > +{ + #[ inline( always ) ] + fn clone( &self ) -> Self + { + Self( self.0, std::marker::PhantomData ) + } +} + +impl< 'a, T, How > Copy for Ref< 'a, T, How > {} +impl< 'a, T, How > Copy for _Ref< 'a, T, How > {} + +// impl< 'a, T, How > AsRef< T > for Ref< 'a, T, How > +// { +// fn as_ref( &self ) -> &T +// { +// &self.0 +// } +// } + +impl< 'a, T, How > Deref for Ref< 'a, T, How > +{ + type Target = _Ref< 'a, T, How >; + fn deref( &self ) -> &Self::Target + { + // panic!( "deref" ); + &self.0 + } +} + +impl< 'a, T, How > From< &'a T > for Ref< 'a, T, How > +{ + fn from( src : &'a T ) -> Self + { + Ref( _Ref( src, std::marker::PhantomData ) ) + } +} diff --git a/module/core/format_tools/src/format/to_string_with_fallback.rs b/module/core/format_tools/src/format/to_string_with_fallback.rs index 795da418c3..0add72e343 100644 --- a/module/core/format_tools/src/format/to_string_with_fallback.rs +++ b/module/core/format_tools/src/format/to_string_with_fallback.rs @@ -6,48 +6,66 @@ pub( crate ) mod private { + use crate::*; + pub use super:: { - aref::ToStringWithFallbackRef, - params::ToStringWithFallbackParams, + aref::{ Ref, _Ref }, }; - use crate::ToStringWith; + use std:: + { + borrow::Cow, + }; // == /// Trait to convert a type to a string with a fallback formatting. - pub trait _ToStringWithFallback< How, Fallback > + pub trait ToStringWithFallback< 'a, How, Fallback > { /// Converts the type to a string using the specified formatting or a fallback. - fn to_string_with_fallback( self ) -> String + fn to_string_with_fallback( self ) -> Cow< 'a, str > ; } - impl< T, How, Fallback > _ToStringWithFallback< How, Fallback > - for ToStringWithFallbackRef< '_, T, ToStringWithFallbackParams< How, Fallback > > + impl< 'a, T, How, Fallback > ToStringWithFallback< 'a, How, Fallback > + for _Ref< 'a, T, How, Fallback > where - T : ToStringWith< Fallback >, + T : ToStringWith< 'a, Fallback >, { /// Converts the type to a string using the specified formatting. - fn to_string_with_fallback( self ) -> String + #[ inline ] + fn to_string_with_fallback( self ) -> Cow< 'a, str > { < T as ToStringWith< Fallback > >::to_string_with( self.0 ) } } - impl< T, How, Fallback > _ToStringWithFallback< How, Fallback > - for &ToStringWithFallbackRef< '_, T, ToStringWithFallbackParams< How, Fallback > > + impl< 'a, T, How, Fallback > ToStringWithFallback< 'a, How, Fallback > + for Ref< 'a, T, How, Fallback > where - T : ToStringWith< How >, + T : ToStringWith< 'a, How >, { /// Converts the type to a string using the fallback formatting. - fn to_string_with_fallback( self ) -> String + #[ inline ] + fn to_string_with_fallback( self ) -> Cow< 'a, str > { - < T as ToStringWith< How > >::to_string_with( self.0 ) + < T as ToStringWith< How > >::to_string_with( self.0.0 ) } } + // impl< T, How, Fallback > ToStringWithFallback< How, Fallback > + // for &Ref< '_, T, How, Fallback > + // where + // T : ToStringWith< How >, + // { + // /// Converts the type to a string using the fallback formatting. + // fn to_string_with_fallback( self ) -> String + // { + // < T as ToStringWith< How > >::to_string_with( self.0 ) + // } + // } + // /// Macro to convert a value to a string using a specified formatting method with a fallback. @@ -99,40 +117,41 @@ pub( crate ) mod private /// /// // Example usage: Using Both which implements both Debug and Display. /// let src = Both; - /// let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + /// let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); /// let exp = "This is display".to_string(); /// // The primary formatting method WithDisplay is used. /// assert_eq!( got, exp ); /// /// // Example usage: Using OnlyDebug which implements only Debug. /// let src = OnlyDebug; - /// let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + /// let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); /// let exp = "This is debug".to_string(); /// // The primary formatting method WithDisplay is not available, so the fallback WithDebug is used. /// assert_eq!( got, exp ); /// ``` - #[ macro_export( local_inner_macros ) ] + // #[ macro_export( local_inner_macros ) ] + #[ macro_export ] macro_rules! to_string_with_fallback { ( $how : ty, $fallback : ty, $src : expr ) => {{ - use format_tools::_ToStringWithFallback; - ( - &format_tools - ::ToStringWithFallbackRef - ::< '_, _, format_tools::ToStringWithFallbackParams< $how, $fallback > > - ::from( &$src ) - ) + use format_tools::ToStringWithFallback; + format_tools + ::to_string_with_fallback + ::Ref + ::< '_, _, $how, $fallback > + // ::< '_, _, format_tools::ToStringWithFallbackParams< $how, $fallback > > + ::from( $src ) .to_string_with_fallback() }}; } + pub use to_string_with_fallback; } mod aref; -mod params; #[ doc( inline ) ] #[ allow( unused_imports ) ] @@ -145,6 +164,12 @@ pub mod own use super::*; #[ doc( inline ) ] pub use orphan::*; + #[ doc( inline ) ] + pub use private:: + { + Ref, + to_string_with_fallback, + }; } /// Orphan namespace of the module. @@ -154,6 +179,20 @@ pub mod orphan use super::*; #[ doc( inline ) ] pub use exposed::*; + + // #[ doc( inline ) ] + // use crate::to_string_with_fallback; + + pub use super::super::to_string_with_fallback; + + #[ doc( inline ) ] + pub use private:: + { + // Ref, + ToStringWithFallback, + // to_string_with_fallback, + }; + } /// Exposed namespace of the module. @@ -161,16 +200,17 @@ pub mod orphan pub mod exposed { use super::*; + #[ doc( inline ) ] + pub use prelude::*; + #[ doc( inline ) ] pub use private:: { - ToStringWithFallbackRef, - ToStringWithFallbackParams, - _ToStringWithFallback, + // Ref, + // ToStringWithFallback, + // to_string_with_fallback, }; - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - use crate::to_string_with_fallback; + } /// Prelude to use essentials: `use my_module::prelude::*`. diff --git a/module/core/format_tools/src/format/to_string_with_fallback/aref.rs b/module/core/format_tools/src/format/to_string_with_fallback/aref.rs index 58f8013440..2669fd6d4c 100644 --- a/module/core/format_tools/src/format/to_string_with_fallback/aref.rs +++ b/module/core/format_tools/src/format/to_string_with_fallback/aref.rs @@ -1,70 +1,92 @@ //! -//! Wrapper to wrap argument for trait `_ToStringWithFallback`. +//! Wrapper to wrap argument for trait `ToStringWithFallback`. //! // use core::fmt; use core::ops::{ Deref }; -/// Transparent reference wrapper emphasizing a specific aspect of identity of its internal type. +/// Reference wrapper to make into string conversion with fallback. #[ allow( missing_debug_implementations ) ] #[ repr( transparent ) ] -pub struct ToStringWithFallbackRef< 'a, T, Marker >( pub &'a T, ::core::marker::PhantomData< fn() -> Marker > ) +pub struct Ref< 'a, T, How, Fallback > +( pub _Ref< 'a, T, How, Fallback > ) where - ::core::marker::PhantomData< fn( Marker ) > : Copy, &'a T : Copy, ; -impl< 'a, T, Marker > ToStringWithFallbackRef< 'a, T, Marker > +/// Internal reference wrapper to make into string conversion with fallback. +#[ allow( missing_debug_implementations ) ] +#[ repr( transparent ) ] +pub struct _Ref< 'a, T, How, Fallback > +( pub &'a T, ::core::marker::PhantomData< fn() -> ( How, Fallback ) > ) +where + ::core::marker::PhantomData< fn() -> ( How, Fallback ) > : Copy, + &'a T : Copy, +; + +impl< 'a, T, How, Fallback > Ref< 'a, T, How, Fallback > { - /// Just a constructor. - #[ inline( always ) ] - pub fn new( src : &'a T ) -> Self - { - Self( src, ::core::marker::PhantomData ) - } + // /// Just a constructor. + // #[ inline( always ) ] + // pub fn new( src : &'a T ) -> Self + // { + // Self( src, ::core::marker::PhantomData ) + // } /// Just a constructor. #[ inline( always ) ] pub fn inner( self ) -> &'a T { - self.0 + self.0.0 } } -impl< 'a, T, Marker > Clone for ToStringWithFallbackRef< 'a, T, Marker > +impl< 'a, T, How, Fallback > Clone for Ref< 'a, T, How, Fallback > { #[ inline( always ) ] fn clone( &self ) -> Self { - Self::new( self.0 ) + Self( self.0 ) } } -impl< 'a, T, Marker > Copy for ToStringWithFallbackRef< 'a, T, Marker > {} - -impl< 'a, T, Marker > AsRef< T > for ToStringWithFallbackRef< 'a, T, Marker > +impl< 'a, T, How, Fallback > Clone for _Ref< 'a, T, How, Fallback > { - fn as_ref( &self ) -> &T + #[ inline( always ) ] + fn clone( &self ) -> Self { - &self.0 + Self( self.0, std::marker::PhantomData ) } } -impl< 'a, T, Marker > Deref for ToStringWithFallbackRef< 'a, T, Marker > +impl< 'a, T, How, Fallback > Copy for Ref< 'a, T, How, Fallback > {} +impl< 'a, T, How, Fallback > Copy for _Ref< 'a, T, How, Fallback > {} + +// impl< 'a, T, How, Fallback > AsRef< T > for Ref< 'a, T, How, Fallback > +// { +// fn as_ref( &self ) -> &T +// { +// &self.0 +// } +// } + +impl< 'a, T, How, Fallback > Deref for Ref< 'a, T, How, Fallback > { - type Target = T; + type Target = _Ref< 'a, T, How, Fallback >; fn deref( &self ) -> &Self::Target { &self.0 } } -impl< 'a, T, Marker > From< &'a T > for ToStringWithFallbackRef< 'a, T, Marker > +// xxx2 : wrap into wrap + +impl< 'a, T, How, Fallback > From< &'a T > for Ref< 'a, T, How, Fallback > { fn from( src : &'a T ) -> Self { - ToStringWithFallbackRef::new( src ) + Ref( _Ref( src, std::marker::PhantomData ) ) } } diff --git a/module/core/format_tools/src/format/to_string_with_fallback/params.rs b/module/core/format_tools/src/format/to_string_with_fallback/params.rs deleted file mode 100644 index 1b901ec99c..0000000000 --- a/module/core/format_tools/src/format/to_string_with_fallback/params.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! -//! Marker type for trait `_ToStringWithFallback` with type parameters. -//! - -/// Marker type for trait `_ToStringWithFallback` with type parameters. -#[ derive( Debug, Default, Clone, Copy ) ] -pub struct ToStringWithFallbackParams< How, Fallback >( ::core::marker::PhantomData< fn() -> ( How, Fallback ) > ); diff --git a/module/core/format_tools/src/format/wrapper.rs b/module/core/format_tools/src/format/wrapper.rs index 288a561e25..510ddd8420 100644 --- a/module/core/format_tools/src/format/wrapper.rs +++ b/module/core/format_tools/src/format/wrapper.rs @@ -43,7 +43,7 @@ pub mod exposed { aref::IntoRef, aref::Ref, - maybe_as::IntoMaybeAs, + // maybe_as::IntoMaybeAs, maybe_as::MaybeAs, }; } diff --git a/module/core/format_tools/src/format/wrapper/maybe_as.rs b/module/core/format_tools/src/format/wrapper/maybe_as.rs index d9c4a910c3..71701be306 100644 --- a/module/core/format_tools/src/format/wrapper/maybe_as.rs +++ b/module/core/format_tools/src/format/wrapper/maybe_as.rs @@ -6,36 +6,45 @@ use core::fmt; use std::borrow::Cow; use core::ops::{ Deref }; -/// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. -pub trait IntoMaybeAs< 'a, T, Marker > -where - T : Clone, -{ - /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker >; -} - -impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for T -where - T : Clone, -{ - #[ inline( always ) ] - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > - { - MaybeAs::< 'a, T, Marker >::new( self ) - } -} +// // xxx : review +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// pub trait IntoMaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// +// { +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker >; +// } +// +// impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for < T as std::borrow::ToOwned >::Owned +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// #[ inline( always ) ] +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > +// { +// MaybeAs::< 'a, T, Marker >::new( self ) +// } +// } -impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for &'a T -where - T : Clone, -{ - #[ inline( always ) ] - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > - { - MaybeAs::< 'a, T, Marker >::new_with_ref( self ) - } -} +// impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for &'a T +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// +// { +// #[ inline( always ) ] +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > +// { +// MaybeAs::< 'a, T, Marker >::new_with_ref( self ) +// } +// } // xxx // impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for () @@ -51,18 +60,44 @@ where /// Universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. #[ repr( transparent ) ] -#[ derive( Clone ) ] +// #[ derive( Clone ) ] pub struct MaybeAs< 'a, T, Marker >( pub Option< Cow< 'a, T > >, ::core::marker::PhantomData< fn() -> Marker > ) where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, ; impl< 'a, T, Marker > MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, { - /// Just a constructor. + /// Check is it borrowed. + #[ inline( always ) ] + pub fn is_borrowed( &self ) -> bool + { + if self.0.is_none() + { + return false; + } + match self.0.as_ref().unwrap() + { + Cow::Borrowed( _ ) => true, + Cow::Owned( _ ) => false, + } + } + + /// Check does it have some value. + #[ inline( always ) ] + pub fn is_some( &self ) -> bool + { + return self.0.is_some() + } + + /// Constructor returning none. #[ inline( always ) ] pub fn none() -> Self { @@ -71,11 +106,12 @@ where /// Just a constructor. #[ inline( always ) ] - pub fn new( src : T ) -> Self + pub fn new( src : < T as std::borrow::ToOwned >::Owned ) -> Self { Self( Some( Cow::Owned( src ) ), ::core::marker::PhantomData ) } + // xxx : review /// Just a constructor. #[ inline( always ) ] pub fn new_with_ref( src : &'a T ) -> Self @@ -99,10 +135,24 @@ where } +impl< 'a, T, Marker > Clone for MaybeAs< 'a, T, Marker > +where + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, +{ + fn clone( &self ) -> Self + { + Self( self.0.clone(), ::core::marker::PhantomData ) + } +} + impl< 'a, T, Marker > AsRef< Option< Cow< 'a, T > > > for MaybeAs< 'a, T, Marker > where - T : Clone, - Self : 'a, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // Self : 'a, { fn as_ref( &self ) -> &Option< Cow< 'a, T > > { @@ -112,8 +162,10 @@ where impl< 'a, T, Marker > Deref for MaybeAs< 'a, T, Marker > where - T : Clone, - Marker : 'static, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + // T : Clone, + Marker : Clone + Copy + 'static, { type Target = Option< Cow< 'a, T > >; fn deref( &self ) -> &Option< Cow< 'a, T > > @@ -155,21 +207,53 @@ where // } // } -impl< 'a, T, Marker > From< T > +// impl< 'a, T, Marker > From< &T > +// for MaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// fn from( src : &T ) -> Self +// { +// MaybeAs::new( src ) +// } +// } + +// impl< 'a, T, Marker > From< < T as std::borrow::ToOwned >::Owned > +// for MaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// fn from( src : < T as std::borrow::ToOwned >::Owned ) -> Self +// { +// MaybeAs::new( src ) +// } +// } + +impl< 'a, T, Marker > From< Cow< 'a, T > > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // T : Clone, { - fn from( src : T ) -> Self + fn from( src : Cow< 'a, T > ) -> Self { - MaybeAs::new( src ) + MaybeAs::new_with_inner( Some( src ) ) } } impl< 'a, T, Marker > From< Option< Cow< 'a, T > > > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // T : Clone, { fn from( src : Option< Cow< 'a, T > > ) -> Self { @@ -180,7 +264,9 @@ where impl< 'a, T, Marker > From< &'a T > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, { fn from( src : &'a T ) -> Self { @@ -212,19 +298,23 @@ where impl< 'a, T, Marker > Default for MaybeAs< 'a, T, Marker > where - T : Clone, - T : Default, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + < T as std::borrow::ToOwned >::Owned : Default, + Marker : Clone + Copy + 'static, { fn default() -> Self { - MaybeAs::new( T::default() ) + MaybeAs::new( < T as std::borrow::ToOwned >::Owned::default() ) } } impl< 'a, T, Marker > fmt::Debug for MaybeAs< 'a, T, Marker > where + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone + fmt::Debug, + Marker : Clone + Copy + 'static, T : fmt::Debug, - T : Clone, { fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result { @@ -236,7 +326,10 @@ where impl< 'a, T, Marker > PartialEq for MaybeAs< 'a, T, Marker > where - T : Clone + PartialEq, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + T : PartialEq, { fn eq( &self, other : &Self ) -> bool { @@ -246,6 +339,9 @@ where impl< 'a, T, Marker > Eq for MaybeAs< 'a, T, Marker > where - T : Clone + Eq, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + T : Eq, { } diff --git a/module/core/format_tools/src/lib.rs b/module/core/format_tools/src/lib.rs index 9fea5c4ace..bfd4a579f8 100644 --- a/module/core/format_tools/src/lib.rs +++ b/module/core/format_tools/src/lib.rs @@ -30,8 +30,10 @@ pub mod own pub use orphan::*; #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use super::format::orphan::*; + pub use super::format::own::*; + + // #[ doc( inline ) ] + // pub use super::format::orphan::*; } @@ -41,6 +43,10 @@ pub mod own pub mod orphan { use super::*; + + #[ doc( inline ) ] + pub use super::format::orphan::*; + #[ doc( inline ) ] pub use exposed::*; } @@ -56,11 +62,12 @@ pub mod exposed pub use prelude::*; #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::format::exposed::*; + // #[ doc( inline ) ] + // pub use super::format::exposed::*; + #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::dependency::reflect_tools:: { Fields, @@ -75,7 +82,11 @@ pub mod exposed pub mod prelude { use super::*; + #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::format::prelude::*; + + // #[ doc( inline ) ] + // pub use super::format::prelude::*; + } diff --git a/module/core/format_tools/tests/inc/fields_test.rs b/module/core/format_tools/tests/inc/fields_test.rs index 3e655d27af..b837ccafcc 100644 --- a/module/core/format_tools/tests/inc/fields_test.rs +++ b/module/core/format_tools/tests/inc/fields_test.rs @@ -28,83 +28,171 @@ pub struct TestObject pub tools : Option< Vec< HashMap< String, String > > >, } -impl< 'a, How > Fields< 'a, &'static str, MaybeAs< 'a, String, How > > +use the_module::to_string_with_fallback; +use the_module::to_string_with_fallback::ToStringWithFallback; + +impl< 'a, How > Fields< 'a, &'static str, MaybeAs< 'a, str, How > > for TestObject where + // MaybeAs< 'a, str, How > : Clone, // xxx How : Clone + Copy + 'static, - String : ToStringWith< How >, - i64 : ToStringWith< How >, - Vec< String > : ToStringWith< How >, - Vec< HashMap< String, String > > : ToStringWith< How >, + + // String : ToStringWith< 'a, How >, + // i64 : ToStringWith< 'a, How >, + // Vec< String > : ToStringWith< 'a, How >, + // Vec< HashMap< String, String > > : ToStringWith< 'a, How >, + + // to_string_with_fallback::Ref< 'a, String, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, + // to_string_with_fallback::Ref< 'a, i64, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, + // to_string_with_fallback::Ref< 'a, Vec< String >, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, + // to_string_with_fallback::Ref< 'a, Vec< HashMap< String, String > >, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, + { - fn fields( &'a self ) -> impl IteratorTrait< Item = ( &'static str, MaybeAs< 'a, String, How > ) > + fn fields( &'a self ) -> impl IteratorTrait< Item = ( &'static str, MaybeAs< 'a, str, How > ) > { - let mut dst : Vec< ( &'static str, MaybeAs< 'a, String, How > ) > = Vec::new(); - fn from< 'a, V, How >( src : &'a V ) -> MaybeAs< 'a, String, How > - where - How : Clone + Copy + 'static, - V : ToStringWith< How > + 'a, + let mut dst : Vec< ( &'static str, MaybeAs< 'a, str, How > ) > = Vec::new(); + + // fn from< 'a, V, How >( src : &'a V ) -> MaybeAs< 'a, str, How > + // where + // How : Clone + Copy + 'static, + // // V : ToStringWith< 'a, How > + 'a, + // // to_string_with_fallback::Ref< 'a, V, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, + // { + // MaybeAs::< 'a, str, How >::from + // ( + // to_string_with_fallback!( How, WithDebug, src ) + // // < V as ToStringWith< 'a, How > >::to_string_with( src ) + // ) + // } + +// fn add< 'a, V, How > +// ( +// dst : &mut Vec< ( &'static str, MaybeAs< 'a, str, How > ) >, +// key : &'static str, +// src : &'a V +// ) +// where +// How : Clone + Copy + 'static, +// // V : ToStringWith< 'a, How > + 'a, +// to_string_with_fallback::Ref< 'a, V, How, WithDebug > : ToStringWithFallback< 'a, How, WithDebug > + 'a, +// { +// let val = from( src ); +// dst.push( ( key, val ) ); +// } + + #[ macro_export( local_inner_macros ) ] + macro_rules! into_maybe_as { - MaybeAs::< 'a, String, How >::from ( - < V as ToStringWith< How > >::to_string_with( src ) + $src : expr ) + => + {{ + format_tools::MaybeAs::< 'a, str, How >::from + ( + format_tools::to_string_with_fallback!( How, WithDebug, $src ) + ) + }}; } - fn add< 'a, V, How > - ( - dst : &mut Vec< ( &'static str, MaybeAs< 'a, String, How > ) >, - key : &'static str, - src : &'a V - ) - where - How : Clone + Copy + 'static, - V : ToStringWith< How > + 'a, - { - let val = from( src ); - dst.push( ( key, val ) ); - } - - dst.push( ( "id", from( &self.id ) ) ); - // add( &mut dst, "id", &self.id ); - // dst.push( ( "id", MaybeAs::< 'a, String, How >::from( &self.id ) ) ); - add( &mut dst, "created_at", &self.created_at ); - add( &mut dst, "file_ids", &self.file_ids ); + dst.push( ( "id", into_maybe_as!( &self.id ) ) ); + dst.push( ( "created_at", into_maybe_as!( &self.created_at ) ) ); + dst.push( ( "file_ids", into_maybe_as!( &self.file_ids ) ) ); if let Some( tools ) = &self.tools { - add( &mut dst, "tools", tools ); + dst.push( ( "tools", into_maybe_as!( tools ) ) ); } else { dst.push( ( "tools", MaybeAs::none() ) ); } + // dst.push( ( "id", MaybeAs::< 'a, str, How >::from( to_string_with_fallback!( How, WithDebug, &self.id ) ) ) ); + + // dst.push( ( "id", from( &self.id ) ) ); + +// // add( &mut dst, "id", &self.id ); +// // dst.push( ( "id", MaybeAs::< 'a, String, How >::from( &self.id ) ) ); +// add( &mut dst, "created_at", &self.created_at ); +// add( &mut dst, "file_ids", &self.file_ids ); +// +// if let Some( tools ) = &self.tools +// { +// add( &mut dst, "tools", tools ); +// } +// else +// { +// dst.push( ( "tools", MaybeAs::none() ) ); +// } + dst.into_iter() } } // -#[ allow( dead_code ) ] -fn is_borrowed< 'a, T : Clone >( src : &Option< Cow< 'a, T > > ) -> bool +// #[ allow( dead_code ) ] +// fn is_borrowed< 'a, T >( src : &Option< Cow< 'a, T > > ) -> bool +// where +// T : std::borrow::ToOwned + ?Sized, +// { +// if src.is_none() +// { +// return false; +// } +// match src.as_ref().unwrap() +// { +// Cow::Borrowed( _ ) => true, +// Cow::Owned( _ ) => false, +// } +// } + +// + +#[ test ] +fn basic_with_debug() { - if src.is_none() - { - return false; - } - match src.as_ref().unwrap() + let test_object = TestObject { - Cow::Borrowed( _ ) => true, - Cow::Owned( _ ) => false, - } + id : "12345".to_string(), + created_at : 1627845583, + file_ids : vec![ "file1".to_string(), "file2".to_string() ], + tools : Some + ( + vec! + [{ + let mut map = HashMap::new(); + map.insert( "tool1".to_string(), "value1".to_string() ); + map.insert( "tool2".to_string(), "value2".to_string() ); + map + }] + ), + }; + + let fields : Vec< ( &str, MaybeAs< '_, str, WithDebug > ) > = + Fields::< '_, &'static str, MaybeAs< '_, str, WithDebug > >::fields( &test_object ).collect(); + + let fields : Vec< ( &str, MaybeAs< '_, str, WithDebug > ) > = test_object.fields().collect(); + + assert_eq!( fields.len(), 4 ); + assert!( !fields[ 0 ].1.is_borrowed() ); + assert!( !fields[ 1 ].1.is_borrowed() ); + assert!( !fields[ 2 ].1.is_borrowed() ); + assert!( !fields[ 3 ].1.is_borrowed() ); + assert_eq!( fields[ 0 ], ( "id", Some( Cow::Borrowed( "\"12345\"" ) ).into() ) ); + assert_eq!( fields[ 0 ], ( "id", Some( Cow::Owned( "\"12345\"".to_string() ) ).into() ) ); + assert_eq!( fields[ 1 ], ( "created_at", Some( Cow::Owned( "1627845583".to_string() ) ).into() ) ); + assert_eq!( fields[ 2 ], ( "file_ids", Some( Cow::Owned( "[\"file1\", \"file2\"]".to_string() ) ).into() ) ); + assert_eq!( fields[ 3 ].0, "tools" ); + } // #[ test ] -fn basic() +fn basic_with_display() { let test_object = TestObject { @@ -123,15 +211,18 @@ fn basic() ), }; - let fields : Vec< ( &str, MaybeAs< '_, String, WithDebug > ) > = test_object.fields().collect(); + let fields : Vec< ( &str, MaybeAs< '_, str, WithDisplay > ) > = + Fields::< '_, &'static str, MaybeAs< '_, str, WithDisplay > >::fields( &test_object ).collect(); + + let fields : Vec< ( &str, MaybeAs< '_, str, WithDisplay > ) > = test_object.fields().collect(); assert_eq!( fields.len(), 4 ); - // assert!( is_borrowed( &fields[ 0 ].1 ) ); - // assert!( !is_borrowed( &fields[ 1 ].1 ) ); - // assert!( !is_borrowed( &fields[ 2 ].1 ) ); - // assert!( !is_borrowed( &fields[ 3 ].1 ) ); - // xxx - assert_eq!( fields[ 0 ], ( "id", Some( Cow::Borrowed( &"\"12345\"".to_string() ) ).into() ) ); + assert!( fields[ 0 ].1.is_borrowed() ); + assert!( !fields[ 1 ].1.is_borrowed() ); + assert!( !fields[ 2 ].1.is_borrowed() ); + assert!( !fields[ 3 ].1.is_borrowed() ); + assert_eq!( fields[ 0 ], ( "id", Some( Cow::Borrowed( "\"12345\"" ) ).into() ) ); + assert_eq!( fields[ 0 ], ( "id", Some( Cow::Owned( "\"12345\"".to_string() ) ).into() ) ); assert_eq!( fields[ 1 ], ( "created_at", Some( Cow::Owned( "1627845583".to_string() ) ).into() ) ); assert_eq!( fields[ 2 ], ( "file_ids", Some( Cow::Owned( "[\"file1\", \"file2\"]".to_string() ) ).into() ) ); assert_eq!( fields[ 3 ].0, "tools" ); @@ -177,3 +268,5 @@ fn test_vec_fields() assert_eq!( fields[ 1 ].0, 1 ); } + +// xxx : fix the test diff --git a/module/core/format_tools/tests/inc/to_string_example.rs b/module/core/format_tools/tests/inc/to_string_example.rs index 2bc356a052..637892daac 100644 --- a/module/core/format_tools/tests/inc/to_string_example.rs +++ b/module/core/format_tools/tests/inc/to_string_example.rs @@ -44,12 +44,12 @@ fn exmaple() } let src = Both; - let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); let exp = "This is display".to_string(); assert_eq!( got, exp ); let src = OnlyDebug; - let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); let exp = "This is debug".to_string(); assert_eq!( got, exp ); diff --git a/module/core/format_tools/tests/inc/to_string_test.rs b/module/core/format_tools/tests/inc/to_string_test.rs index 4d2ed8cdd9..baffd0ba71 100644 --- a/module/core/format_tools/tests/inc/to_string_test.rs +++ b/module/core/format_tools/tests/inc/to_string_test.rs @@ -8,6 +8,11 @@ use the_module:: WithDisplay, }; +use std:: +{ + borrow::Cow, +}; + // #[ test ] @@ -41,3 +46,57 @@ fn to_string_with_test() // - } + +// + +#[ test ] +fn borrowed() +{ + + let src = 13; + let got = ToStringWith::< WithDisplay >::to_string_with( &src ); + let exp : Cow< '_, str > = Cow::Owned( "13".to_string() ); + a_id!( got, exp ); + a_true!( matches!( got, Cow::Owned( _ ) ) ); + + let src = "str"; + let got = ToStringWith::< WithDisplay >::to_string_with( &src ); + let exp : Cow< '_, str > = Cow::Borrowed( "str" ); + a_id!( got, exp ); + a_true!( matches!( got, Cow::Borrowed( _ ) ) ); + + let src = "string".to_string(); + let got = ToStringWith::< WithDisplay >::to_string_with( &src ); + let exp : Cow< '_, str > = Cow::Borrowed( "string" ); + a_id!( got, exp ); + a_true!( matches!( got, Cow::Borrowed( _ ) ) ); + +} + +// + +#[ test ] +fn borrowed_str() +{ + + let src = "str"; + let got = ToStringWith::< WithDisplay >::to_string_with( &src ); + let exp : Cow< '_, str > = Cow::Borrowed( "str" ); + a_id!( got, exp ); + a_true!( matches!( got, Cow::Borrowed( _ ) ) ); + +} + +// + +#[ test ] +fn borrowed_string() +{ + + let src = "string".to_string(); + let got = ToStringWith::< WithDisplay >::to_string_with( &src ); + let exp : Cow< '_, str > = Cow::Borrowed( "string" ); + a_id!( got, exp ); + a_true!( matches!( got, Cow::Borrowed( _ ) ) ); + +} diff --git a/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs b/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs index 7a1a97fda2..b2de94f5bf 100644 --- a/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs +++ b/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs @@ -3,30 +3,37 @@ use super::*; use the_module:: { - _ToStringWithFallback, - ToStringWithFallbackParams, + ToStringWithFallback, + // ToStringWithFallbackParams, WithDebug, WithDisplay, - ToStringWithFallbackRef, + // the_module::to_string_with_fallback::Ref, to_string_with_fallback, }; +use std:: +{ + // fmt, + // collections::HashMap, + borrow::Cow, +}; + // #[ test ] fn to_string_with_fallback_basic() { - // - ToStringWithFallbackRef should implement copy + // - the_module::to_string_with_fallback::Ref should implement copy - fn f1( _src : ToStringWithFallbackRef::< '_, Struct1, ToStringWithFallbackParams< WithDisplay, WithDebug > > ) + fn f1( _src : the_module::to_string_with_fallback::Ref::< '_, Struct1, WithDisplay, WithDebug > ) where - for< 'a > ToStringWithFallbackRef::< 'a, Struct1, ToStringWithFallbackParams< WithDisplay, WithDebug > > : Copy + Clone, + for< 'a > the_module::to_string_with_fallback::Ref::< 'a, Struct1, WithDisplay, WithDebug > : Copy + Clone, {} struct Struct1; let src = Struct1; - let ref1 = ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ); + let ref1 = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ); let ref2 = ref1; f1( ref1 ); f1( ref2 ); @@ -34,12 +41,12 @@ fn to_string_with_fallback_basic() // - let src = 13i32; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ).to_string_with_fallback(); let exp = "13".to_string(); a_id!( got, exp ); let src = "abc".to_string(); - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ).to_string_with_fallback(); let exp = "abc".to_string(); a_id!( got, exp ); @@ -65,7 +72,7 @@ fn to_string_with_fallback_variants() } let src = OnlyDisplay; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ).to_string_with_fallback(); let exp = "This is display".to_string(); a_id!( got, exp ); @@ -82,15 +89,15 @@ fn to_string_with_fallback_variants() } let src = OnlyDebug; - let _ref1 = ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ); + let _ref1 = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ); let src = OnlyDebug; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ).to_string_with_fallback(); let exp = "This is debug".to_string(); a_id!( got, exp ); let src = OnlyDebug; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDebug, WithDisplay > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDebug, WithDisplay >::from( &src ).to_string_with_fallback(); let exp = "This is debug".to_string(); a_id!( got, exp ); @@ -115,12 +122,12 @@ fn to_string_with_fallback_variants() } let src = Both; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDisplay, WithDebug > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDisplay, WithDebug >::from( &src ).to_string_with_fallback(); let exp = "This is display".to_string(); a_id!( got, exp ); let src = Both; - let got = ( &ToStringWithFallbackRef::< '_, _, ToStringWithFallbackParams< WithDebug, WithDisplay > >::from( &src ) ).to_string_with_fallback(); + let got = the_module::to_string_with_fallback::Ref::< '_, _, WithDebug, WithDisplay >::from( &src ).to_string_with_fallback(); let exp = "This is debug".to_string(); a_id!( got, exp ); @@ -147,12 +154,12 @@ fn to_string_with_fallback_macro() } let src = OnlyDebug; - let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); let exp = "This is debug".to_string(); a_id!( got, exp ); let src = OnlyDebug; - let got = to_string_with_fallback!( WithDebug, WithDisplay, src ); + let got = to_string_with_fallback!( WithDebug, WithDisplay, &src ); let exp = "This is debug".to_string(); a_id!( got, exp ); @@ -177,13 +184,36 @@ fn to_string_with_fallback_macro() } let src = Both; - let got = to_string_with_fallback!( WithDisplay, WithDebug, src ); + let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); let exp = "This is display".to_string(); a_id!( got, exp ); let src = Both; - let got = to_string_with_fallback!( WithDebug, WithDisplay, src ); + let got = to_string_with_fallback!( WithDebug, WithDisplay, &src ); let exp = "This is debug".to_string(); a_id!( got, exp ); } + +// + +#[ test ] +fn display_is_not_implemented() +{ + + let src = vec![ 1, 2, 3 ]; + let got = the_module + ::to_string_with_fallback + ::Ref + ::< '_, _, WithDisplay, WithDebug > + ::from( &src ) + .to_string_with_fallback(); + let exp : Cow< '_, String > = Cow::Owned( "[1, 2, 3]".to_string() ); + a_id!( got, exp ); + + let src = vec![ 1, 2, 3 ]; + let got = to_string_with_fallback!( WithDisplay, WithDebug, &src ); + let exp : Cow< '_, String > = Cow::Owned( "[1, 2, 3]".to_string() ); + a_id!( got, exp ); + +} diff --git a/module/core/reflect_tools/Cargo.toml b/module/core/reflect_tools/Cargo.toml index 8fb902cee9..c13e7b544d 100644 --- a/module/core/reflect_tools/Cargo.toml +++ b/module/core/reflect_tools/Cargo.toml @@ -28,20 +28,18 @@ all-features = false default = [ "enabled", - "reflect_reflect", + "reflect_types", "reflect_newtype", ] full = [ - "enabled", - "reflect_reflect", - "reflect_newtype", + "default", ] enabled = [ "reflect_tools_meta/enabled", "derive_tools/enabled" ] -reflect_reflect = [] +reflect_types = [] reflect_newtype = [] [dependencies] diff --git a/module/core/reflect_tools/src/lib.rs b/module/core/reflect_tools/src/lib.rs index 67edd9c9de..f4f71a2a2a 100644 --- a/module/core/reflect_tools/src/lib.rs +++ b/module/core/reflect_tools/src/lib.rs @@ -1,11 +1,11 @@ -#![ cfg_attr( feature = "no_std", no_std ) ] +// #![ cfg_attr( feature = "no_std", no_std ) ] #![ doc( html_logo_url = "https://raw.githubusercontent.com/Wandalen/wTools/master/asset/img/logo_v3_trans_square.png" ) ] #![ doc( html_favicon_url = "https://raw.githubusercontent.com/Wandalen/wTools/alpha/asset/img/logo_v3_trans_square_icon_small_v2.ico" ) ] #![ doc( html_root_url = "https://docs.rs/reflect_tools/latest/reflect_tools/" ) ] #![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "Readme.md" ) ) ] #[ cfg( feature = "enabled" ) ] -#[ cfg( feature = "reflect_reflect" ) ] +#[ cfg( feature = "reflect_types" ) ] pub mod reflect; /// Namespace with dependencies. @@ -13,7 +13,7 @@ pub mod reflect; #[ cfg( feature = "enabled" ) ] pub mod dependency { - #[ cfg( any_derive ) ] + // #[ cfg( any_derive ) ] pub use ::reflect_tools_meta; } @@ -30,9 +30,8 @@ pub mod own use super::*; #[ doc( inline ) ] pub use orphan::*; - #[ cfg( feature = "reflect_reflect" ) ] + #[ cfg( feature = "reflect_types" ) ] #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::reflect::orphan::*; } @@ -55,14 +54,12 @@ pub mod exposed #[ doc( inline ) ] pub use prelude::*; - #[ cfg( feature = "reflect_reflect" ) ] + #[ cfg( feature = "reflect_types" ) ] #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::reflect::exposed::*; // #[ cfg( any_derive ) ] #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use ::reflect_tools_meta::*; } @@ -74,9 +71,8 @@ pub mod prelude { use super::*; - #[ cfg( feature = "reflect_reflect" ) ] + #[ cfg( feature = "reflect_types" ) ] #[ doc( inline ) ] - #[ allow( unused_imports ) ] pub use super::reflect::prelude::*; } diff --git a/module/core/reflect_tools/src/reflect/fields.rs b/module/core/reflect_tools/src/reflect/fields.rs index 4ec4548a13..3410fcf5a1 100644 --- a/module/core/reflect_tools/src/reflect/fields.rs +++ b/module/core/reflect_tools/src/reflect/fields.rs @@ -49,21 +49,8 @@ pub( crate ) mod private { /// Returns an iterator over all fields of the specified type within the entity. fn fields( &'a self ) -> impl IteratorTrait< Item = ( K, V ) >; - // fn fields( &'a self ) -> impl IteratorTrait< Item = ( K, Option< Cow< 'a, V > > ) >; } - // /// Return number of fields convertible into a specified type withing an entity. - // /// - // /// # Type Parameters - // /// - // /// - `V`: The value type. - // /// - // pub trait FieldsLen< V > - // { - // /// Return number of fields convertible into a specified type withing an entity. - // fn len( &self ) -> usize; - // } - /// Trait returning name of type of variable. pub trait TypeName { diff --git a/module/core/reflect_tools/src/reflect/wrapper.rs b/module/core/reflect_tools/src/reflect/wrapper.rs index 288a561e25..fc18dd958a 100644 --- a/module/core/reflect_tools/src/reflect/wrapper.rs +++ b/module/core/reflect_tools/src/reflect/wrapper.rs @@ -41,9 +41,9 @@ pub mod exposed #[ allow( unused_imports ) ] pub use super:: { - aref::IntoRef, - aref::Ref, - maybe_as::IntoMaybeAs, + // aref::IntoRef, + // aref::Ref, + // maybe_as::IntoMaybeAs, maybe_as::MaybeAs, }; } diff --git a/module/core/reflect_tools/src/reflect/wrapper/aref.rs b/module/core/reflect_tools/src/reflect/wrapper/aref.rs index 0be38207e0..891a479ec2 100644 --- a/module/core/reflect_tools/src/reflect/wrapper/aref.rs +++ b/module/core/reflect_tools/src/reflect/wrapper/aref.rs @@ -1,118 +1,118 @@ -//! -//! It's often necessary to wrap something inot a local structure and this file contains a resusable local structure for wrapping. -//! - -// xxx : make command to autogenerate it, maybe - -// use core::fmt; -use core::ops::{ Deref }; - -/// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. -pub trait IntoRef< 'a, T, Marker > -{ - /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. - fn into_ref( self ) -> Ref< 'a, T, Marker >; -} - -impl< 'a, T, Marker > IntoRef< 'a, T, Marker > for &'a T -{ - #[ inline( always ) ] - fn into_ref( self ) -> Ref< 'a, T, Marker > - { - Ref::< 'a, T, Marker >::new( self ) - } -} - -/// Transparent reference wrapper emphasizing a specific aspect of identity of its internal type. -#[ allow( missing_debug_implementations ) ] -#[ repr( transparent ) ] -pub struct Ref< 'a, T, Marker >( pub &'a T, ::core::marker::PhantomData< fn() -> Marker > ) -where - ::core::marker::PhantomData< fn( Marker ) > : Copy, - &'a T : Copy, -; - -impl< 'a, T, Marker > Clone for Ref< 'a, T, Marker > -{ - #[ inline( always ) ] - fn clone( &self ) -> Self - { - Self::new( self.0 ) - } -} - -impl< 'a, T, Marker > Copy for Ref< 'a, T, Marker > {} - -impl< 'a, T, Marker > Ref< 'a, T, Marker > -{ - - /// Just a constructor. - #[ inline( always ) ] - pub fn new( src : &'a T ) -> Self - { - Self( src, ::core::marker::PhantomData ) - } - - /// Just a constructor. - #[ inline( always ) ] - pub fn inner( self ) -> &'a T - { - self.0 - } - -} - -impl< 'a, T, Marker > AsRef< T > for Ref< 'a, T, Marker > -{ - fn as_ref( &self ) -> &T - { - &self.0 - } -} - -impl< 'a, T, Marker > Deref for Ref< 'a, T, Marker > -{ - type Target = T; - fn deref( &self ) -> &Self::Target - { - &self.0 - } -} - -impl< 'a, T, Marker > From< &'a T > for Ref< 'a, T, Marker > -{ - fn from( src : &'a T ) -> Self - { - Ref::new( src ) - } -} - -// impl< 'a, T, Marker > From< Ref< 'a, T, Marker > > for &'a T +// //! +// //! It's often necessary to wrap something inot a local structure and this file contains a resusable local structure for wrapping. +// //! +// +// // xxx : make command to autogenerate it, maybe +// +// // use core::fmt; +// use core::ops::{ Deref }; +// +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// pub trait IntoRef< 'a, T, Marker > // { -// fn from( wrapper : Ref< 'a, T, Marker > ) -> &'a T +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// fn into_ref( self ) -> Ref< 'a, T, Marker >; +// } +// +// impl< 'a, T, Marker > IntoRef< 'a, T, Marker > for &'a T +// { +// #[ inline( always ) ] +// fn into_ref( self ) -> Ref< 'a, T, Marker > // { -// wrapper.0 +// Ref::< 'a, T, Marker >::new( self ) // } // } - -// impl< 'a, T, Marker > Default for Ref< 'a, T, Marker > +// +// /// Transparent reference wrapper emphasizing a specific aspect of identity of its internal type. +// #[ allow( missing_debug_implementations ) ] +// #[ repr( transparent ) ] +// pub struct Ref< 'a, T, Marker >( pub &'a T, ::core::marker::PhantomData< fn() -> Marker > ) // where -// T : Default, +// ::core::marker::PhantomData< fn( Marker ) > : Copy, +// &'a T : Copy, +// ; +// +// impl< 'a, T, Marker > Clone for Ref< 'a, T, Marker > // { -// fn default() -> Self +// #[ inline( always ) ] +// fn clone( &self ) -> Self // { -// Ref( &T::default() ) +// Self::new( self.0 ) // } // } - -// impl< 'a, T, Marker > fmt::Debug for Ref< 'a, T, Marker > -// where -// T : fmt::Debug, +// +// impl< 'a, T, Marker > Copy for Ref< 'a, T, Marker > {} +// +// impl< 'a, T, Marker > Ref< 'a, T, Marker > +// { +// +// /// Just a constructor. +// #[ inline( always ) ] +// pub fn new( src : &'a T ) -> Self +// { +// Self( src, ::core::marker::PhantomData ) +// } +// +// /// Just a constructor. +// #[ inline( always ) ] +// pub fn inner( self ) -> &'a T +// { +// self.0 +// } +// +// } +// +// impl< 'a, T, Marker > AsRef< T > for Ref< 'a, T, Marker > +// { +// fn as_ref( &self ) -> &T +// { +// &self.0 +// } +// } +// +// impl< 'a, T, Marker > Deref for Ref< 'a, T, Marker > +// { +// type Target = T; +// fn deref( &self ) -> &Self::Target +// { +// &self.0 +// } +// } +// +// impl< 'a, T, Marker > From< &'a T > for Ref< 'a, T, Marker > // { -// fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result +// fn from( src : &'a T ) -> Self // { -// f.debug_struct( "Ref" ) -// .field( "0", &self.0 ) -// .finish() +// Ref::new( src ) // } // } +// +// // impl< 'a, T, Marker > From< Ref< 'a, T, Marker > > for &'a T +// // { +// // fn from( wrapper : Ref< 'a, T, Marker > ) -> &'a T +// // { +// // wrapper.0 +// // } +// // } +// +// // impl< 'a, T, Marker > Default for Ref< 'a, T, Marker > +// // where +// // T : Default, +// // { +// // fn default() -> Self +// // { +// // Ref( &T::default() ) +// // } +// // } +// +// // impl< 'a, T, Marker > fmt::Debug for Ref< 'a, T, Marker > +// // where +// // T : fmt::Debug, +// // { +// // fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result +// // { +// // f.debug_struct( "Ref" ) +// // .field( "0", &self.0 ) +// // .finish() +// // } +// // } diff --git a/module/core/reflect_tools/src/reflect/wrapper/maybe_as.rs b/module/core/reflect_tools/src/reflect/wrapper/maybe_as.rs index d9c4a910c3..71701be306 100644 --- a/module/core/reflect_tools/src/reflect/wrapper/maybe_as.rs +++ b/module/core/reflect_tools/src/reflect/wrapper/maybe_as.rs @@ -6,36 +6,45 @@ use core::fmt; use std::borrow::Cow; use core::ops::{ Deref }; -/// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. -pub trait IntoMaybeAs< 'a, T, Marker > -where - T : Clone, -{ - /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker >; -} - -impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for T -where - T : Clone, -{ - #[ inline( always ) ] - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > - { - MaybeAs::< 'a, T, Marker >::new( self ) - } -} +// // xxx : review +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// pub trait IntoMaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// +// { +// /// Converter into universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker >; +// } +// +// impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for < T as std::borrow::ToOwned >::Owned +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// #[ inline( always ) ] +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > +// { +// MaybeAs::< 'a, T, Marker >::new( self ) +// } +// } -impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for &'a T -where - T : Clone, -{ - #[ inline( always ) ] - fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > - { - MaybeAs::< 'a, T, Marker >::new_with_ref( self ) - } -} +// impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for &'a T +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// +// { +// #[ inline( always ) ] +// fn into_maybe_as( self ) -> MaybeAs< 'a, T, Marker > +// { +// MaybeAs::< 'a, T, Marker >::new_with_ref( self ) +// } +// } // xxx // impl< 'a, T, Marker > IntoMaybeAs< 'a, T, Marker > for () @@ -51,18 +60,44 @@ where /// Universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type. #[ repr( transparent ) ] -#[ derive( Clone ) ] +// #[ derive( Clone ) ] pub struct MaybeAs< 'a, T, Marker >( pub Option< Cow< 'a, T > >, ::core::marker::PhantomData< fn() -> Marker > ) where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, ; impl< 'a, T, Marker > MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, { - /// Just a constructor. + /// Check is it borrowed. + #[ inline( always ) ] + pub fn is_borrowed( &self ) -> bool + { + if self.0.is_none() + { + return false; + } + match self.0.as_ref().unwrap() + { + Cow::Borrowed( _ ) => true, + Cow::Owned( _ ) => false, + } + } + + /// Check does it have some value. + #[ inline( always ) ] + pub fn is_some( &self ) -> bool + { + return self.0.is_some() + } + + /// Constructor returning none. #[ inline( always ) ] pub fn none() -> Self { @@ -71,11 +106,12 @@ where /// Just a constructor. #[ inline( always ) ] - pub fn new( src : T ) -> Self + pub fn new( src : < T as std::borrow::ToOwned >::Owned ) -> Self { Self( Some( Cow::Owned( src ) ), ::core::marker::PhantomData ) } + // xxx : review /// Just a constructor. #[ inline( always ) ] pub fn new_with_ref( src : &'a T ) -> Self @@ -99,10 +135,24 @@ where } +impl< 'a, T, Marker > Clone for MaybeAs< 'a, T, Marker > +where + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, +{ + fn clone( &self ) -> Self + { + Self( self.0.clone(), ::core::marker::PhantomData ) + } +} + impl< 'a, T, Marker > AsRef< Option< Cow< 'a, T > > > for MaybeAs< 'a, T, Marker > where - T : Clone, - Self : 'a, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // Self : 'a, { fn as_ref( &self ) -> &Option< Cow< 'a, T > > { @@ -112,8 +162,10 @@ where impl< 'a, T, Marker > Deref for MaybeAs< 'a, T, Marker > where - T : Clone, - Marker : 'static, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + // T : Clone, + Marker : Clone + Copy + 'static, { type Target = Option< Cow< 'a, T > >; fn deref( &self ) -> &Option< Cow< 'a, T > > @@ -155,21 +207,53 @@ where // } // } -impl< 'a, T, Marker > From< T > +// impl< 'a, T, Marker > From< &T > +// for MaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// fn from( src : &T ) -> Self +// { +// MaybeAs::new( src ) +// } +// } + +// impl< 'a, T, Marker > From< < T as std::borrow::ToOwned >::Owned > +// for MaybeAs< 'a, T, Marker > +// where +// T : std::borrow::ToOwned + ?Sized, +// < T as std::borrow::ToOwned >::Owned : Clone, +// // T : Clone, +// { +// fn from( src : < T as std::borrow::ToOwned >::Owned ) -> Self +// { +// MaybeAs::new( src ) +// } +// } + +impl< 'a, T, Marker > From< Cow< 'a, T > > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // T : Clone, { - fn from( src : T ) -> Self + fn from( src : Cow< 'a, T > ) -> Self { - MaybeAs::new( src ) + MaybeAs::new_with_inner( Some( src ) ) } } impl< 'a, T, Marker > From< Option< Cow< 'a, T > > > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + // T : Clone, { fn from( src : Option< Cow< 'a, T > > ) -> Self { @@ -180,7 +264,9 @@ where impl< 'a, T, Marker > From< &'a T > for MaybeAs< 'a, T, Marker > where - T : Clone, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, { fn from( src : &'a T ) -> Self { @@ -212,19 +298,23 @@ where impl< 'a, T, Marker > Default for MaybeAs< 'a, T, Marker > where - T : Clone, - T : Default, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + < T as std::borrow::ToOwned >::Owned : Default, + Marker : Clone + Copy + 'static, { fn default() -> Self { - MaybeAs::new( T::default() ) + MaybeAs::new( < T as std::borrow::ToOwned >::Owned::default() ) } } impl< 'a, T, Marker > fmt::Debug for MaybeAs< 'a, T, Marker > where + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone + fmt::Debug, + Marker : Clone + Copy + 'static, T : fmt::Debug, - T : Clone, { fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result { @@ -236,7 +326,10 @@ where impl< 'a, T, Marker > PartialEq for MaybeAs< 'a, T, Marker > where - T : Clone + PartialEq, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + T : PartialEq, { fn eq( &self, other : &Self ) -> bool { @@ -246,6 +339,9 @@ where impl< 'a, T, Marker > Eq for MaybeAs< 'a, T, Marker > where - T : Clone + Eq, + T : std::borrow::ToOwned + ?Sized, + < T as std::borrow::ToOwned >::Owned : Clone, + Marker : Clone + Copy + 'static, + T : Eq, { } diff --git a/module/core/reflect_tools/tests/inc/fundamental/fields_test.rs b/module/core/reflect_tools/tests/inc/fundamental/fields_test.rs index 703011a643..3a2667fe64 100644 --- a/module/core/reflect_tools/tests/inc/fundamental/fields_test.rs +++ b/module/core/reflect_tools/tests/inc/fundamental/fields_test.rs @@ -10,6 +10,8 @@ use the_module:: // WithDebug, }; +// xxx2 : check + use std:: { // fmt, @@ -53,19 +55,19 @@ for TestObject // -#[ allow( dead_code ) ] -fn is_borrowed< 'a, T : Clone >( src : &Option< Cow< 'a, T > > ) -> bool -{ - if src.is_none() - { - return false; - } - match src.as_ref().unwrap() - { - Cow::Borrowed( _ ) => true, - Cow::Owned( _ ) => false, - } -} +// #[ allow( dead_code ) ] +// fn is_borrowed< 'a, T : Clone >( src : &Option< Cow< 'a, T > > ) -> bool +// { +// if src.is_none() +// { +// return false; +// } +// match src.as_ref().unwrap() +// { +// Cow::Borrowed( _ ) => true, +// Cow::Owned( _ ) => false, +// } +// } // @@ -92,10 +94,10 @@ fn basic() let fields : Vec< ( &str, MaybeAs< '_, String, () > ) > = test_object.fields().collect(); assert_eq!( fields.len(), 4 ); - assert!( is_borrowed( &fields[ 0 ].1 ) ); - assert!( !is_borrowed( &fields[ 1 ].1 ) ); - assert!( !is_borrowed( &fields[ 2 ].1 ) ); - assert!( !is_borrowed( &fields[ 3 ].1 ) ); + assert!( fields[ 0 ].1.is_borrowed() ); + assert!( !fields[ 1 ].1.is_borrowed() ); + assert!( !fields[ 2 ].1.is_borrowed() ); + assert!( !fields[ 3 ].1.is_borrowed() ); assert_eq!( fields[ 0 ], ( "id", Some( Cow::Borrowed( &"12345".to_string() ) ).into() ) ); assert_eq!( fields[ 1 ], ( "created_at", Some( Cow::Owned( "1627845583".to_string() ) ).into() ) ); assert_eq!( fields[ 2 ], ( "file_ids", Some( Cow::Owned( "[\"file1\", \"file2\"]".to_string() ) ).into() ) ); diff --git a/module/core/reflect_tools_meta/Cargo.toml b/module/core/reflect_tools_meta/Cargo.toml index 0ace8e3a22..f77d73c630 100644 --- a/module/core/reflect_tools_meta/Cargo.toml +++ b/module/core/reflect_tools_meta/Cargo.toml @@ -31,15 +31,14 @@ proc-macro = true default = [ "enabled", - "reflect_reflect", + "reflect_derive", ] full = [ - "enabled", - "reflect_reflect", + "default", ] enabled = [] -reflect_reflect = [] +reflect_derive = [] [dependencies] # iter_tools = { workspace = true, features = [ "full" ] } diff --git a/module/core/reflect_tools_meta/src/lib.rs b/module/core/reflect_tools_meta/src/lib.rs index c1e30cd88d..ab0705ae24 100644 --- a/module/core/reflect_tools_meta/src/lib.rs +++ b/module/core/reflect_tools_meta/src/lib.rs @@ -9,16 +9,17 @@ ( any ( - feature = "reflect_reflect", + feature = "reflect_derive", ) )] #[ cfg( feature = "enabled" ) ] mod implementation; + #[ cfg ( any ( - feature = "reflect_reflect", + feature = "reflect_derive", ) )] #[ cfg( feature = "enabled" ) ] @@ -33,7 +34,7 @@ use implementation::*; /// #[ cfg( feature = "enabled" ) ] -#[ cfg( feature = "derive_reflect" ) ] +#[ cfg( feature = "reflect_derive" ) ] #[ proc_macro_derive( Reflect, attributes( debug ) ) ] pub fn derive_reflect( input : proc_macro::TokenStream ) -> proc_macro::TokenStream {