Add Debug implementation

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 2f22893287
commit 8b454cb2f5
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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"

@ -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::<String>().unwrap();
/// println!("{}", string);
/// ```
#[derive(Debug)]
pub struct MultitraitObject {
pub(crate) data: *mut (),
pub(crate) original_typeid: TypeId,

@ -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);

@ -41,6 +41,7 @@ fn it_creates_fat_pointers() {
#[test]
fn it_constructs() {
TestStruct::default().into_multitrait();
String::from("test").into_multitrait();
}
#[test]

@ -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::<dyn Debug>() {
debug.fmt(f)
} else {
write!(f, "<unavailable>")
}
}
}

@ -1,2 +1,5 @@
mod try_clone;
mod debug;
pub use try_clone::*;
pub use debug::*;

@ -27,6 +27,8 @@ impl TryClone for MultitraitObject {
fn try_clone(&self) -> Option<Self> {
let clone = self.downcast_trait::<dyn RawClone>()?;
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 {

Loading…
Cancel
Save