diff --git a/Cargo.toml b/Cargo.toml index 6ee1679..db28c1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "multi-trait-object" -version = "0.1.0" +version = "0.1.1" edition = "2021" readme = "README.md" license = "Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 16d3da0..6c76cfc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,10 @@ mod tests; pub(crate) mod macros; mod trait_impl; +mod std_impl; + pub use trait_impl::*; +pub use std_impl::*; use std::any::{Any, TypeId}; use std::collections::HashMap; @@ -33,7 +36,6 @@ pub struct FatPointer { /// let string = mto.downcast::().unwrap(); /// println!("{}", string); /// ``` -#[derive(Debug)] pub struct MultitraitObject { pub(crate) data: *mut (), pub(crate) original_typeid: TypeId, diff --git a/src/std_impl.rs b/src/std_impl.rs new file mode 100644 index 0000000..d67b84f --- /dev/null +++ b/src/std_impl.rs @@ -0,0 +1,6 @@ +use std::fmt::{Debug, Display}; +use std::fmt::Write as FmtWrite; + +use crate::*; + +impl_trait_object!(String, dyn Debug, dyn Display, dyn RawClone, dyn FmtWrite); diff --git a/src/tests.rs b/src/tests.rs index a0ed773..4459eff 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -41,6 +41,7 @@ fn it_creates_fat_pointers() { #[test] fn it_constructs() { TestStruct::default().into_multitrait(); + String::from("test").into_multitrait(); } #[test] diff --git a/src/trait_impl/debug.rs b/src/trait_impl/debug.rs new file mode 100644 index 0000000..6ebf27c --- /dev/null +++ b/src/trait_impl/debug.rs @@ -0,0 +1,12 @@ +use std::fmt::{Debug, Formatter}; +use crate::MultitraitObject; + +impl Debug for MultitraitObject { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if let Some(debug) = self.downcast_trait::() { + debug.fmt(f) + } else { + write!(f, "") + } + } +} \ No newline at end of file diff --git a/src/trait_impl/mod.rs b/src/trait_impl/mod.rs index 45779ea..56cc473 100644 --- a/src/trait_impl/mod.rs +++ b/src/trait_impl/mod.rs @@ -1,2 +1,5 @@ mod try_clone; -pub use try_clone::*; \ No newline at end of file +mod debug; + +pub use try_clone::*; +pub use debug::*; \ No newline at end of file diff --git a/src/trait_impl/try_clone.rs b/src/trait_impl/try_clone.rs index 94609c1..8eba46f 100644 --- a/src/trait_impl/try_clone.rs +++ b/src/trait_impl/try_clone.rs @@ -27,6 +27,8 @@ impl TryClone for MultitraitObject { fn try_clone(&self) -> Option { let clone = self.downcast_trait::()?; let data_ptr = unsafe { + // SAFETY: We're using the pointer in the multitrait object so + // it won't leak memory clone.raw_clone() }; Some(MultitraitObject {