pub struct SliceView<'a> { /* private fields */ }Expand description
A non-owning view of a contiguous memory region, borrowed for 'a: a base
address and a length (both raw u64s).
SliceView is Copy and freely duplicated — it borrows, it does not own.
The lifetime 'a bounds the region’s validity: as_bytes
/ as_bytes_mut hand back &'a [u8] /
&'a mut [u8], so a byte slice can never outlive the region. Get one by
borrowing an owning Slice with view (then 'a is that
borrow), or, for raw memory, with the unsafe
new (then 'a is the caller’s promise). Deriving a
sub-view (subslice, split_at)
is safe and preserves 'a.
Implementations§
Source§impl<'a> SliceView<'a>
impl<'a> SliceView<'a>
Sourcepub const unsafe fn new(ptr: u64, len: u64) -> Self
pub const unsafe fn new(ptr: u64, len: u64) -> Self
Constructs a view from a base address and a length, borrowed for 'a.
§Safety
The region [ptr, ptr + len) must be valid memory the caller may access
for the whole of 'a — which the caller chooses here, so it is a
promise the caller must keep. Reading it back through
as_bytes / as_bytes_mut
dereferences it. Prefer Slice::view, which fixes 'a to the owner’s
borrow instead. The null view needs no unsafe.
Sourcepub const fn end(&self) -> u64
pub const fn end(&self) -> u64
The address one past the end of the region (ptr + len, wrapping).
Sourcepub const fn range(&self) -> Range<u64> ⓘ
pub const fn range(&self) -> Range<u64> ⓘ
The region as a half-open address range ptr .. ptr + len.
Sourcepub const fn contains(&self, addr: u64) -> bool
pub const fn contains(&self, addr: u64) -> bool
Returns true if addr lies within [ptr, ptr + len).
Sourcepub const fn covers(&self, other: &SliceView<'_>) -> bool
pub const fn covers(&self, other: &SliceView<'_>) -> bool
Returns true if other’s range lies entirely within this one.
Sourcepub const fn overlaps(&self, other: &SliceView<'_>) -> bool
pub const fn overlaps(&self, other: &SliceView<'_>) -> bool
Returns true if the two regions share any address.
Sourcepub const fn is_aligned_to(&self, align: u64) -> bool
pub const fn is_aligned_to(&self, align: u64) -> bool
Returns true if the base address is a multiple of align
(false if align is 0).
Sourcepub const fn as_ptr(&self) -> *const u8
pub const fn as_ptr(&self) -> *const u8
The base address as a *const u8. Producing the pointer is safe;
dereferencing it is not.
Sourcepub const fn as_mut_ptr(&self) -> *mut u8
pub const fn as_mut_ptr(&self) -> *mut u8
The base address as a *mut u8. Producing the pointer is safe;
dereferencing it is not.
Sourcepub const fn subslice(&self, offset: u64, len: u64) -> Option<SliceView<'a>>
pub const fn subslice(&self, offset: u64, len: u64) -> Option<SliceView<'a>>
A sub-view [offset, offset + len) of this region, or None if that
range does not fit within [0, self.len). The sub-view shares this
region’s lifetime 'a.
Safe: the parent view already bounds its region to 'a, and the result
is a SliceView (not an owner), so it can never be mistaken for a
freeable allocation.
Sourcepub const unsafe fn subslice_unchecked(
&self,
offset: u64,
len: u64,
) -> SliceView<'a>
pub const unsafe fn subslice_unchecked( &self, offset: u64, len: u64, ) -> SliceView<'a>
A sub-view [offset, offset + len), without the bounds check.
§Safety
offset + len must not exceed self.len (and must not overflow), so the
result stays within this region.
Sourcepub const fn split_at(&self, mid: u64) -> Option<(SliceView<'a>, SliceView<'a>)>
pub const fn split_at(&self, mid: u64) -> Option<(SliceView<'a>, SliceView<'a>)>
Splits the region at mid bytes into (head, tail), or None if
mid > self.len.
Sourcepub const unsafe fn as_bytes(&self) -> &'a [u8]
pub const unsafe fn as_bytes(&self) -> &'a [u8]
Views the region as a shared byte slice, bounded by the region’s
lifetime 'a.
§Safety
The region must be valid and initialized for len bytes and not be
mutated (through any alias) for 'a.
Sourcepub const unsafe fn as_bytes_mut(&self) -> &'a mut [u8] ⓘ
pub const unsafe fn as_bytes_mut(&self) -> &'a mut [u8] ⓘ
Views the region as an exclusive byte slice, bounded by the region’s
lifetime 'a.
§Safety
The region must be valid and initialized for len bytes and not be
aliased for 'a. (SliceView is Copy, so exclusivity is not enforced
by the borrow checker here — it is the caller’s obligation.)
Sourcepub const unsafe fn to_slice(&self) -> Slice
pub const unsafe fn to_slice(&self) -> Slice
Promotes this view to an owning Slice.
§Safety
The view must name a whole allocation that this allocator produced and
still owns (so it may be handed to free /
realloc). A view into the middle of a
block, or one already owned by another Slice, must not be promoted —
doing so and then freeing it corrupts the heap.