mirror of
https://github.com/fafhrd91/actix-net
synced 2025-02-17 13:33:31 +01:00
Add ByteString::slice_ref (#470)
This commit is contained in:
parent
00654aadc5
commit
363984ad75
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
## Unreleased - 2022-xx-xx
|
## Unreleased - 2022-xx-xx
|
||||||
- Minimum supported Rust version (MSRV) is now 1.57.
|
- Minimum supported Rust version (MSRV) is now 1.57.
|
||||||
|
- Add `ByteString::slice_ref` which can safely slice a `ByteString` into a new one with zero copy. [#470]
|
||||||
|
|
||||||
|
[#470]: https://github.com/actix/actix-net/pull/470
|
||||||
|
|
||||||
## 1.1.0 - 2022-06-11
|
## 1.1.0 - 2022-06-11
|
||||||
- Implement `From<Box<str>>` for `ByteString`. [#458]
|
- Implement `From<Box<str>>` for `ByteString`. [#458]
|
||||||
|
@ -50,6 +50,34 @@ impl ByteString {
|
|||||||
pub const unsafe fn from_bytes_unchecked(src: Bytes) -> ByteString {
|
pub const unsafe fn from_bytes_unchecked(src: Bytes) -> ByteString {
|
||||||
Self(src)
|
Self(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a slice of self that is equivalent to the given `subset`. Corresponds to [`Bytes::slice_ref`].
|
||||||
|
///
|
||||||
|
/// 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 another `ByteString`, as if one had
|
||||||
|
/// sliced the `ByteString` with the offsets that correspond to `subset`.
|
||||||
|
///
|
||||||
|
/// This operation is `O(1)`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use bytestring::ByteString;
|
||||||
|
///
|
||||||
|
/// let string = ByteString::from_static(" foo ");
|
||||||
|
/// let subset = string.trim();
|
||||||
|
/// let substring = string.slice_ref(subset);
|
||||||
|
/// assert_eq!(substring, "foo");
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Requires that the given `subset` str is in fact contained within the
|
||||||
|
/// `ByteString` buffer; otherwise this function will panic.
|
||||||
|
pub fn slice_ref(&self, subset: &str) -> Self {
|
||||||
|
Self(self.0.slice_ref(subset.as_bytes()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<str> for ByteString {
|
impl PartialEq<str> for ByteString {
|
||||||
@ -349,4 +377,10 @@ mod test {
|
|||||||
let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap();
|
let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap();
|
||||||
assert_eq!(s, r#""nice bytes""#);
|
assert_eq!(s, r#""nice bytes""#);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_slice_ref_catches_not_a_subset() {
|
||||||
|
ByteString::from_static("foo bar").slice_ref("foo");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user