Skip to content

Commit

Permalink
AUTO : Forward from derives_refactoring_8 to alpha (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtools-bot authored Jul 8, 2024
1 parent e667a7e commit 09109be
Show file tree
Hide file tree
Showing 22 changed files with 1,061 additions and 422 deletions.
2 changes: 1 addition & 1 deletion module/core/format_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
167 changes: 145 additions & 22 deletions module/core/format_tools/src/format/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
pub( crate ) mod private
{

pub use super::
{
aref::{ Ref, _Ref },
};

use std::
{
fmt,
borrow::Cow,
};

// ==
Expand All @@ -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::*;
Expand All @@ -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.
Expand All @@ -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::
{
Expand All @@ -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::*`.
Expand Down
93 changes: 93 additions & 0 deletions module/core/format_tools/src/format/to_string/aref.rs
Original file line number Diff line number Diff line change
@@ -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 ) )
}
}
Loading

0 comments on commit 09109be

Please sign in to comment.