zerocopy::byte_slice

Trait SplitByteSlice

Source
pub unsafe trait SplitByteSlice: ByteSlice {
    // Required method
    unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self);

    // Provided method
    fn split_at(self, mid: usize) -> Result<(Self, Self), Self> { ... }
}
Expand description

A ByteSlice that can be split in two.

§Safety

Unsafe code may depend for its soundness on the assumption that split_at and split_at_unchecked are implemented correctly. In particular, given B: SplitByteSlice and b: B, if b.deref() returns a byte slice with address addr and length len, then if split <= len, both of these invocations:

  • b.split_at(split)
  • b.split_at_unchecked(split)

…will return (first, second) such that:

  • first’s address is addr and its length is split
  • second’s address is addr + split and its length is len - split

Required Methods§

Source

unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self)

Splits the slice at the midpoint, possibly omitting bounds checks.

s.split_at_unchecked(mid) returns s[..mid] and s[mid..].

§Safety

mid must not be greater than self.deref().len().

§Panics

Implementations of this method may choose to perform a bounds check and panic if mid > self.deref().len(). They may also panic for any other reason. Since it is optional, callers must not rely on this behavior for soundness.

Provided Methods§

Source

fn split_at(self, mid: usize) -> Result<(Self, Self), Self>

Attempts to split self at the midpoint.

s.split_at(mid) returns Ok((s[..mid], s[mid..])) if mid <= s.deref().len() and otherwise returns Err(s).

§Safety

Unsafe code may rely on this function correctly implementing the above functionality.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl SplitByteSlice for &[u8]

Source§

unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self)

Source§

impl SplitByteSlice for &mut [u8]

Source§

unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self)

Source§

impl SplitByteSlice for Ref<'_, [u8]>

Source§

unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self)

Source§

impl SplitByteSlice for RefMut<'_, [u8]>

Source§

unsafe fn split_at_unchecked(self, mid: usize) -> (Self, Self)

Implementors§