mirror of
https://github.com/fafhrd91/actix-net
synced 2025-03-20 16:05:18 +01:00
impl AsRef<ByteString>
This commit is contained in:
parent
045cf3f3e8
commit
878d3a1c74
@ -1,6 +1,7 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2022-xx-xx
|
## Unreleased - 2022-xx-xx
|
||||||
|
- Implement `AsRef<ByteString>` for `ByteString`.
|
||||||
- Minimum supported Rust version (MSRV) is now 1.59.
|
- Minimum supported Rust version (MSRV) is now 1.59.
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,22 +51,26 @@ impl ByteString {
|
|||||||
Self(src)
|
Self(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a byte string that is equivalent to the given `subset`.
|
/// Returns a new byte string that is equivalent to the given `subset`.
|
||||||
///
|
///
|
||||||
/// When processing a `ByteString` buffer with other tools, one often gets a `&str` which is in
|
/// When processing a `ByteString` buffer with other tools, one often gets a `&str` which is in
|
||||||
/// fact a slice of the `ByteString`; i.e., a subset of it. This function turns that `&str` into
|
/// fact a slice of the original `ByteString`; i.e., a subset of it. This function turns that
|
||||||
/// another `ByteString`, as if one had sliced the `ByteString` with the offsets that correspond
|
/// `&str` into another `ByteString`, as if one had sliced the `ByteString` with the offsets
|
||||||
/// to `subset`.
|
/// that correspond to `subset`.
|
||||||
///
|
///
|
||||||
/// Corresponds to [`Bytes::slice_ref`].
|
/// Corresponds to [`Bytes::slice_ref`].
|
||||||
///
|
///
|
||||||
/// This operation is `O(1)`.
|
/// This operation is `O(1)`.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// Requires that the given `subset` str is in fact contained within the `ByteString` buffer;
|
///
|
||||||
/// otherwise this function will panic.
|
/// Panics if `subset` is not a sub-slice of this byte string.
|
||||||
|
///
|
||||||
|
/// Note that strings which are only subsets from an equality perspective do not uphold this
|
||||||
|
/// requirement; see examples.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use bytestring::ByteString;
|
/// # use bytestring::ByteString;
|
||||||
/// let string = ByteString::from_static(" foo ");
|
/// let string = ByteString::from_static(" foo ");
|
||||||
@ -74,6 +78,13 @@ impl ByteString {
|
|||||||
/// let substring = string.slice_ref(subset);
|
/// let substring = string.slice_ref(subset);
|
||||||
/// assert_eq!(substring, "foo");
|
/// assert_eq!(substring, "foo");
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// ```should_panic
|
||||||
|
/// # use bytestring::ByteString;
|
||||||
|
/// // panics because the given slice is not derived from the original byte string, despite
|
||||||
|
/// // being a logical subset of the string
|
||||||
|
/// ByteString::from_static("foo bar").slice_ref("foo");
|
||||||
|
/// ```
|
||||||
pub fn slice_ref(&self, subset: &str) -> Self {
|
pub fn slice_ref(&self, subset: &str) -> Self {
|
||||||
Self(self.0.slice_ref(subset.as_bytes()))
|
Self(self.0.slice_ref(subset.as_bytes()))
|
||||||
}
|
}
|
||||||
@ -91,6 +102,12 @@ impl<T: AsRef<str>> PartialEq<T> for ByteString {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<ByteString> for ByteString {
|
||||||
|
fn as_ref(&self) -> &ByteString {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsRef<[u8]> for ByteString {
|
impl AsRef<[u8]> for ByteString {
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
self.0.as_ref()
|
self.0.as_ref()
|
||||||
@ -276,7 +293,10 @@ mod serde {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use alloc::borrow::ToOwned;
|
use alloc::borrow::ToOwned;
|
||||||
use core::hash::{Hash, Hasher};
|
use core::{
|
||||||
|
hash::{Hash, Hasher},
|
||||||
|
panic::{RefUnwindSafe, UnwindSafe},
|
||||||
|
};
|
||||||
|
|
||||||
use ahash::AHasher;
|
use ahash::AHasher;
|
||||||
use static_assertions::assert_impl_all;
|
use static_assertions::assert_impl_all;
|
||||||
@ -286,16 +306,7 @@ mod test {
|
|||||||
assert_impl_all!(ByteString: Send, Sync, Unpin, Sized);
|
assert_impl_all!(ByteString: Send, Sync, Unpin, Sized);
|
||||||
assert_impl_all!(ByteString: Clone, Default, Eq, PartialOrd, Ord);
|
assert_impl_all!(ByteString: Clone, Default, Eq, PartialOrd, Ord);
|
||||||
assert_impl_all!(ByteString: fmt::Debug, fmt::Display);
|
assert_impl_all!(ByteString: fmt::Debug, fmt::Display);
|
||||||
|
assert_impl_all!(ByteString: UnwindSafe, RefUnwindSafe);
|
||||||
#[rustversion::since(1.56)]
|
|
||||||
mod above_1_56_impls {
|
|
||||||
// `[Ref]UnwindSafe` traits were only in std until rust 1.56
|
|
||||||
|
|
||||||
use core::panic::{RefUnwindSafe, UnwindSafe};
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
assert_impl_all!(ByteString: UnwindSafe, RefUnwindSafe);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_partial_eq() {
|
fn test_partial_eq() {
|
||||||
@ -377,9 +388,20 @@ mod test {
|
|||||||
assert_eq!(s, r#""nice bytes""#);
|
assert_eq!(s, r#""nice bytes""#);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slice_ref() {
|
||||||
|
let string = ByteString::from_static(" foo ");
|
||||||
|
let subset = string.trim();
|
||||||
|
// subset is derived from original byte string
|
||||||
|
let substring = string.slice_ref(subset);
|
||||||
|
assert_eq!(substring, "foo");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_ref_catches_not_a_subset() {
|
fn test_slice_ref_catches_not_a_subset() {
|
||||||
|
// panics because the given slice is not derived from the original byte string, despite
|
||||||
|
// being a logical subset of the string
|
||||||
ByteString::from_static("foo bar").slice_ref("foo");
|
ByteString::from_static("foo bar").slice_ref("foo");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user