Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 81 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ anstyle = { version = "1.0.1", optional = true }
is_terminal_polyfill = { version = "1.48.0", optional = true }
memchr = { version = "2.5", optional = true, default-features = false }
terminal_size = { version = "0.4.0", optional = true }
zerocopy = { version = "0.8.26", features = ["derive"] }

[dev-dependencies]
proptest = "1.2.0"
Expand Down
19 changes: 8 additions & 11 deletions src/stream/bstr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::num::NonZeroUsize;

use zerocopy::{FromBytes, Immutable, KnownLayout};

use crate::error::Needed;
use crate::lib::std::iter::{Cloned, Enumerate};
use crate::lib::std::slice::Iter;
Expand All @@ -20,20 +22,15 @@ use crate::stream::UpdateSlice;

/// Improved `Debug` experience for `&[u8]` UTF-8-ish streams
#[allow(clippy::derived_hash_with_manual_eq)]
#[derive(Hash)]
#[repr(transparent)]
#[derive(Hash, FromBytes, KnownLayout, Immutable)]
#[repr(C)]
pub struct BStr([u8]);

impl BStr {
/// Make a stream out of a byte slice-like.
#[inline]
pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &Self {
Self::from_bytes(bytes.as_ref())
}

#[inline]
fn from_bytes(slice: &[u8]) -> &Self {
unsafe { crate::lib::std::mem::transmute(slice) }
zerocopy::transmute_ref!(bytes.as_ref())
}

#[inline]
Expand Down Expand Up @@ -104,7 +101,7 @@ impl<'i> Stream for &'i BStr {
#[inline(always)]
fn next_slice(&mut self, offset: usize) -> Self::Slice {
let (slice, next) = self.0.split_at(offset);
*self = BStr::from_bytes(next);
*self = zerocopy::transmute_ref!(next);
slice
}
#[inline(always)]
Expand All @@ -116,7 +113,7 @@ impl<'i> Stream for &'i BStr {
let slice = unsafe { self.0.get_unchecked(..offset) };
// SAFETY: `Stream::next_slice_unchecked` requires `offset` to be in bounds
let next = unsafe { self.0.get_unchecked(offset..) };
*self = BStr::from_bytes(next);
*self = zerocopy::transmute_ref!(next);
slice
}
#[inline(always)]
Expand Down Expand Up @@ -368,7 +365,7 @@ impl crate::lib::std::borrow::ToOwned for BStr {
impl crate::lib::std::borrow::Borrow<BStr> for crate::lib::std::vec::Vec<u8> {
#[inline]
fn borrow(&self) -> &BStr {
BStr::from_bytes(self.as_slice())
zerocopy::transmute_ref!(self.as_slice())
}
}

Expand Down
Loading
Loading