From e3c058c96eb5333da915aa83d124409d21c25d6f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 29 Oct 2017 22:50:21 -0700 Subject: [PATCH] convert from ref string into body --- src/body.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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());