1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-28 00:42:58 +01:00

add helper conversions

This commit is contained in:
Nikolay Kim 2019-12-07 10:22:08 +06:00
parent fc0825fcdd
commit 99fef4f06b

View File

@ -90,6 +90,24 @@ impl TryFrom<Vec<u8>> for ByteString {
} }
} }
impl TryFrom<Bytes> for ByteString {
type Error = str::Utf8Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
let _ = str::from_utf8(value.as_ref())?;
Ok(ByteString(value))
}
}
impl TryFrom<bytes::BytesMut> for ByteString {
type Error = str::Utf8Error;
fn try_from(value: bytes::BytesMut) -> Result<Self, Self::Error> {
let _ = str::from_utf8(value.as_ref())?;
Ok(ByteString(value.freeze()))
}
}
macro_rules! array_impls { macro_rules! array_impls {
($($len:expr)+) => { ($($len:expr)+) => {
$( $(
@ -139,7 +157,17 @@ mod test {
} }
#[test] #[test]
fn test_try_from_bytes() { fn test_try_from_rbytes() {
let _ = ByteString::try_from(b"nice bytes").unwrap(); let _ = ByteString::try_from(b"nice bytes").unwrap();
} }
#[test]
fn test_try_from_bytes() {
let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap();
}
#[test]
fn test_try_from_bytesmut() {
let _ = ByteString::try_from(bytes::BytesMut::from(&b"nice bytes"[..])).unwrap();
}
} }