Skip to main content

SliceView

Struct SliceView 

Source
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>

Source

pub const NULL: SliceView<'a>

The null view: base 0, length 0.

Source

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.

Source

pub const fn ptr(&self) -> u64

The base address.

Source

pub const fn len(&self) -> u64

The length, in bytes.

Source

pub const fn is_null(&self) -> bool

Returns true for the null view (base 0).

Source

pub const fn is_empty(&self) -> bool

Returns true when the region is empty (length 0).

Source

pub const fn end(&self) -> u64

The address one past the end of the region (ptr + len, wrapping).

Source

pub const fn range(&self) -> Range<u64> ⓘ

The region as a half-open address range ptr .. ptr + len.

Source

pub const fn contains(&self, addr: u64) -> bool

Returns true if addr lies within [ptr, ptr + len).

Source

pub const fn covers(&self, other: &SliceView<'_>) -> bool

Returns true if other’s range lies entirely within this one.

Source

pub const fn overlaps(&self, other: &SliceView<'_>) -> bool

Returns true if the two regions share any address.

Source

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).

Source

pub const fn as_ptr(&self) -> *const u8

The base address as a *const u8. Producing the pointer is safe; dereferencing it is not.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.)

Source

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.

Trait Implementations§

Source§

impl<'a> Clone for SliceView<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for SliceView<'a>

Source§

impl Debug for SliceView<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for SliceView<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> Eq for SliceView<'a>

Source§

impl From<Slice> for SliceView<'static>

Source§

fn from(s: Slice) -> SliceView<'static>

Consumes the owner, yielding an unbounded view (see Slice::into_view).

Source§

impl<'a> Hash for SliceView<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for SliceView<'a>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<'a> PartialEq for SliceView<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> PartialOrd for SliceView<'a>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> StructuralPartialEq for SliceView<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for SliceView<'a>

§

impl<'a> RefUnwindSafe for SliceView<'a>

§

impl<'a> Send for SliceView<'a>

§

impl<'a> Sync for SliceView<'a>

§

impl<'a> Unpin for SliceView<'a>

§

impl<'a> UnsafeUnpin for SliceView<'a>

§

impl<'a> UnwindSafe for SliceView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.