mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-28 04:12:40 +01:00
add helper conversions
This commit is contained in:
parent
fc0825fcdd
commit
99fef4f06b
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user