diff --git a/src/body.rs b/src/body.rs index 9e8b3ecec..70da53cdd 100644 --- a/src/body.rs +++ b/src/body.rs @@ -106,6 +106,12 @@ impl From for BinaryBody { } } +impl<'a> From<&'a String> for BinaryBody { + fn from(s: &'a String) -> BinaryBody { + BinaryBody::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s))) + } +} + impl From for BinaryBody { fn from(s: Bytes) -> BinaryBody { BinaryBody::Bytes(s) @@ -219,6 +225,13 @@ mod tests { assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); } + #[test] + fn test_ref_string() { + let b = Rc::new("test".to_owned()); + assert_eq!(BinaryBody::from(&b).len(), 4); + assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + } + #[test] fn test_rc_string() { let b = Rc::new("test".to_owned());