1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-03-29 01:37:22 +01:00
axum/src/util.rs
2021-06-15 23:18:49 +02:00

20 lines
540 B
Rust

use bytes::Bytes;
/// A string like type backed by `Bytes` making it cheap to clone.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct ByteStr(Bytes);
impl ByteStr {
pub(crate) fn new<S>(s: S) -> Self
where
S: AsRef<str>,
{
Self(Bytes::copy_from_slice(s.as_ref().as_bytes()))
}
pub(crate) fn as_str(&self) -> &str {
// `ByteStr` can only be constructed from strings which are always valid
// utf-8 so this wont panic.
std::str::from_utf8(&self.0).unwrap()
}
}