mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
Add TryFrom<&Bytes> and TryFrom<&BytesMut> for ByteString
This commit is contained in:
parent
71ec7f8b60
commit
6534dc4d1f
4 changed files with 35 additions and 3 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changes
|
||||
|
||||
## [0.1.17] (2022-12-09)
|
||||
|
||||
* Add TryFrom<&Bytes> and TryFrom<&BytesMut> for ByteString
|
||||
|
||||
## [0.1.16] (2022-07-07)
|
||||
|
||||
* Add ByteString::clear() method
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-bytes"
|
||||
version = "0.1.16"
|
||||
version = "0.1.17"
|
||||
license = "MIT"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Carl Lerche <me@carllerche.com>"]
|
||||
description = "Types and traits for working with bytes (bytes crate fork)"
|
||||
|
@ -22,7 +22,7 @@ bitflags = "1.3"
|
|||
bytes = "1.0.0"
|
||||
serde = "1.0.0"
|
||||
futures-core = { version = "0.3", default-features = false, features = ["alloc"] }
|
||||
simdutf8 = { version = "0.1.3", optional = true }
|
||||
simdutf8 = { version = "0.1.4", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_test = "1.0"
|
||||
|
|
|
@ -277,6 +277,19 @@ impl TryFrom<Bytes> for ByteString {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Bytes> for ByteString {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
|
||||
if utf8::is_valid(value) {
|
||||
Ok(ByteString(value.clone()))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<BytesMut> for ByteString {
|
||||
type Error = ();
|
||||
|
||||
|
@ -290,6 +303,19 @@ impl TryFrom<BytesMut> for ByteString {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a BytesMut> for ByteString {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: &'a BytesMut) -> Result<Self, Self::Error> {
|
||||
if utf8::is_valid(value) {
|
||||
Ok(ByteString(value.clone().freeze()))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<BytesVec> for ByteString {
|
||||
type Error = ();
|
||||
|
||||
|
@ -432,7 +458,9 @@ mod test {
|
|||
let _ = ByteString::try_from(&b"nice bytes"[..]).unwrap();
|
||||
let _ = ByteString::try_from(b"nice bytes".to_vec()).unwrap();
|
||||
let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap();
|
||||
let _ = ByteString::try_from(&Bytes::from_static(b"nice bytes")).unwrap();
|
||||
let _ = ByteString::try_from(BytesMut::from(&b"nice bytes"[..])).unwrap();
|
||||
let _ = ByteString::try_from(&BytesMut::from(&b"nice bytes"[..])).unwrap();
|
||||
let _ =
|
||||
ByteString::try_from(BytesVec::copy_from_slice(&b"nice bytes"[..])).unwrap();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue