macro_rules! impl_try_writeable_delegate {
($ty:ty, |&$self:ident| $delegate:expr, Error = $error:ty $(, |$error_arg:ident| $error_map:expr)? $(, #[$alloc_feature:meta] fn try_write_to_string)? $(, where $($generics:tt)*)?) => { ... };
}Expand description
Macro to implement TryWriteable by delegating to another TryWriteable.
Useful for wrapper types.
ยงExamples
struct MyStruct(Result<String, String>);
writeable::impl_try_writeable_delegate!(
MyStruct,
|&self| &self.0,
Error = String
);
writeable::assert_try_writeable_eq!(
MyStruct(Ok("hello".to_string())),
"hello"
);With an error mapping fn:
struct MyStruct(Result<String, String>);
#[derive(Debug, PartialEq)]
struct MyError;
writeable::impl_try_writeable_delegate!(
MyStruct,
|&self| &self.0,
Error = MyError,
|_error| MyError
);
writeable::assert_try_writeable_eq!(
MyStruct(Ok("hello".to_string())),
"hello"
);
writeable::assert_try_writeable_eq!(
MyStruct(Err("hello".to_string())),
"hello",
Err(MyError)
);With a cfg on fn write_to_string:
struct MyStruct(Result<String, String>);
writeable::impl_try_writeable_delegate!(MyStruct, |&self| &self.0, Error = String, #[cfg(feature = "alloc")] fn try_write_to_string);
writeable::assert_try_writeable_eq!(
MyStruct(Ok("hello".to_string())),
"hello"
);With generics:
use writeable::Writeable;
struct MyStruct<T>(Result<T, T>);
writeable::impl_try_writeable_delegate!(MyStruct<T>, |&self| &self.0, Error = T, where T: Writeable + Clone);
writeable::assert_try_writeable_eq!(
MyStruct(Ok("hello".to_string())),
"hello"
);Implement both Writeable and TryWriteable:
use writeable::adapters::LossyWrap;
// The LossyWrap needs to be a field of MyStruct since it can be borrowed from.
struct MyStruct(LossyWrap<Result<String, String>>);
writeable::impl_try_writeable_delegate!(MyStruct, |&self| &self.0.0, Error = String);
writeable::impl_writeable_delegate!(MyStruct, |&self| &self.0);
writeable::impl_display_with_writeable!(MyStruct);
writeable::assert_try_writeable_eq!(
MyStruct(LossyWrap(Ok("hello".to_string()))),
"hello"
);
writeable::assert_writeable_eq!(
MyStruct(LossyWrap(Ok("hello".to_string()))),
"hello"
);