Struct Split
struct Split<T> { ... }
A T that has been split into two possibly-overlapping parts.
For some dynamically sized types, the padding that appears after the
trailing slice field is a dynamic function of the trailing slice
length. If T is split at a length that
requires trailing padding, the trailing padding of the left part of the
split T will overlap the right part. If T is a mutable reference or
permits interior mutation, you must ensure that the left and right parts do
not overlap. You can do this at zero-cost using using
Self::via_immutable, Self::via_into_bytes, or
Self::via_unaligned, or with a dynamic check by using
Self::via_runtime_check.
Implementations
impl<'a, T> Split<&'a T>
fn via_immutable(self: Self) -> (&'a T, &'a [<T as >::Elem]) where T: ImmutableProduces the split parts of
self, usingImmutableto ensure that it is sound to have concurrent references to both parts.Examples
use ; # use *; // These bytes encode a `Packet`. let bytes = &; let packet = ref_from_bytes.unwrap; assert_eq!; assert_eq!; // Attempt to split `packet` at `length`. let split = packet.split_at.unwrap; // Use the `Immutable` bound on `Packet` to prove that it's okay to // return concurrent references to `packet` and `rest`. let = split.via_immutable; assert_eq!; assert_eq!; assert_eq!;fn via_into_bytes(self: Self) -> (&'a T, &'a [<T as >::Elem]) where T: IntoBytesProduces the split parts of
self, usingIntoBytesto ensure that it is sound to have concurrent references to both parts.Examples
use ; # use *; // These bytes encode a `Packet`. let bytes = &; let packet = ref_from_bytes.unwrap; assert_eq!; assert_eq!; // Attempt to split `packet` at `length`. let split = packet.split_at.unwrap; // Use the `IntoBytes` bound on `Packet` to prove that it's okay to // return concurrent references to `packet` and `rest`. let = split.via_into_bytes; assert_eq!; assert_eq!; assert_eq!;fn via_unaligned(self: Self) -> (&'a T, &'a [<T as >::Elem]) where T: UnalignedProduces the split parts of
self, usingUnalignedto ensure that it is sound to have concurrent references to both parts.Examples
use ; # use *; // These bytes encode a `Packet`. let bytes = &; let packet = ref_from_bytes.unwrap; assert_eq!; assert_eq!; // Attempt to split `packet` at `length`. let split = packet.split_at.unwrap; // Use the `Unaligned` bound on `Packet` to prove that it's okay to // return concurrent references to `packet` and `rest`. let = split.via_unaligned; assert_eq!; assert_eq!; assert_eq!;fn via_runtime_check(self: Self) -> Result<(&'a T, &'a [<T as >::Elem]), Self>Produces the split parts of
self, using a dynamic check to ensure that it is sound to have concurrent references to both parts. You should prefer usingSelf::via_immutable,Self::via_into_bytes, orSelf::via_unaligned, which have no runtime cost.Note that this check is overly conservative if
TisImmutable; for some types, this check will reject some splits whichSelf::via_immutablewill accept.Examples
use ; # use *; // These bytes encode a `Packet`. let bytes = ; let packet = ref_from_bytes.unwrap; assert_eq!; assert_eq!; // Attempt to split `packet` at `length`. let split = packet.split_at.unwrap; // Use a dynamic check to prove that it's okay to return concurrent // references to `packet` and `rest`. let = split.via_runtime_check.unwrap; assert_eq!; assert_eq!; assert_eq!; // Attempt to split `packet` at `length - 1`. let idx = packet.length.get - 1; let split = packet.split_at.unwrap; // Attempt (and fail) to use a dynamic check to prove that it's okay // to return concurrent references to `packet` and `rest`. Note that // this is a case of `via_runtime_check` being overly conservative. // Although the left and right parts indeed overlap, the `Immutable` // bound ensures that concurrently referencing these overlapping // parts is sound. assert!;unsafe fn via_unchecked(self: Self) -> (&'a T, &'a [<T as >::Elem])Unsafely produces the split parts of
self.Safety
If
Tpermits interior mutation, the trailing padding bytes of the left portion must not overlap the right portion. For some dynamically sized types, the padding that appears after the trailing slice field is a dynamic function of the trailing slice length. Thus, for some types, this condition is dependent on the length of the left portion.
impl<'a, T> Split<&'a mut T>
fn via_into_bytes(self: Self) -> (&'a mut T, &'a mut [<T as >::Elem]) where T: IntoBytesProduces the split parts of
self, usingIntoBytesto ensure that it is sound to have concurrent references to both parts.Examples
use ; # use *; // These bytes encode a `Packet`. let mut bytes = &mut ; let packet = mut_from_bytes.unwrap; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn via_unaligned(self: Self) -> (&'a mut T, &'a mut [<T as >::Elem]) where T: UnalignedProduces the split parts of
self, usingUnalignedto ensure that it is sound to have concurrent references to both parts.Examples
use ; # use *; // These bytes encode a `Packet`. let mut bytes = &mut ; let packet = mut_from_bytes.unwrap; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn via_runtime_check(self: Self) -> Result<(&'a mut T, &'a mut [<T as >::Elem]), Self>Produces the split parts of
self, using a dynamic check to ensure that it is sound to have concurrent references to both parts. You should prefer usingSelf::via_into_bytesorSelf::via_unaligned, which have no runtime cost.Examples
use ; # use *; // These bytes encode a `Packet`. let mut bytes = &mut ; let packet = mut_from_bytes.unwrap; assert_eq!; assert_eq!; assert_eq!; assert_eq!;unsafe fn via_unchecked(self: Self) -> (&'a mut T, &'a mut [<T as >::Elem])Unsafely produces the split parts of
self.Safety
The trailing padding bytes of the left portion must not overlap the right portion. For some dynamically sized types, the padding that appears after the trailing slice field is a dynamic function of the trailing slice length. Thus, for some types, this condition is dependent on the length of the left portion.
impl<T> Any for Split<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Split<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Split<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Freeze for Split<T>
impl<T> From for Split<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Split<T>
impl<T> Send for Split<T>
impl<T> Sync for Split<T>
impl<T> Unpin for Split<T>
impl<T> UnsafeUnpin for Split<T>
impl<T> UnwindSafe for Split<T>
impl<T, U> Into for Split<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Split<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Split<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::fmt::Debug> Debug for Split<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result