pub struct BStackSlice<'a, A: BStackAllocator> { /* private fields */ }Expand description
A lifetime-coupled handle to a contiguous region of a BStack payload.
BStackSlice<'a, A> is a lightweight Copy value that holds a shared
reference to the allocator A together with a logical offset and len.
It is the primary handle type produced by BStackAllocator::alloc and
consumed by BStackAllocator::realloc and BStackAllocator::dealloc.
§Lifetime
'a is tied to the allocator borrow, not to the BStack directly.
This means the borrow checker prevents calling
into_stack — which consumes the allocator
by value — while any slice is still alive.
§After dealloc
Once a slice has been passed to BStackAllocator::dealloc, the handle
must not be used for further I/O. The type system enforces this when the
slice is consumed by value, but callers who Copy the handle before
deallocating must uphold this invariant themselves.
Implementations§
Source§impl<'a, A: BStackAllocator> BStackSlice<'a, A>
impl<'a, A: BStackAllocator> BStackSlice<'a, A>
Sourcepub fn new(allocator: &'a A, offset: u64, len: u64) -> Self
👎Deprecated since 0.1.10: Use unsafe { BStackSlice::from_raw_parts(allocator, offset, len) } instead; see BStackSlice::from_raw_parts for the required safety contract.
pub fn new(allocator: &'a A, offset: u64, len: u64) -> Self
Use unsafe { BStackSlice::from_raw_parts(allocator, offset, len) } instead; see BStackSlice::from_raw_parts for the required safety contract.
Create a new BStackSlice.
Does not validate that offset + len <= stack.len(). Invalid slices
produce errors on the first I/O call.
§Deprecation
This constructor is deprecated in favour of the explicitly-unsafe
BStackSlice::from_raw_parts, which makes the caller’s
responsibility visible at the call site. Replace any call
BStackSlice::new(allocator, offset, len) with
unsafe { BStackSlice::from_raw_parts(allocator, offset, len) } and
ensure the # Safety contract of from_raw_parts is upheld.
Sourcepub unsafe fn from_raw_parts(allocator: &'a A, offset: u64, len: u64) -> Self
pub unsafe fn from_raw_parts(allocator: &'a A, offset: u64, len: u64) -> Self
Construct a BStackSlice from raw parts.
This is the explicitly-unsafe replacement for the deprecated
BStackSlice::new. The name reflects that an arbitrary
(offset, len) pair can bypass invariants that allocators rely on.
§Safety
The caller must uphold all of the following:
offset + lenmust not overflowu64.- For I/O calls (
read,write,read_range, etc.) the range[offset, offset + len)should lie within the current payload of the backing stack. Out-of-bounds accesses produceio::Errorrather than unsound behaviour, so this is a correctness requirement, not a soundness one. - If the slice will be passed to
BStackAllocator::reallocorBStackAllocator::dealloc,(offset, len)must describe an allocation that was directly returned byBStackAllocator::allocor by a priorBStackAllocator::reallocon the same allocator instance. Passing an arbitrary offset or a sub-slice derived viasubslice/subslice_rangemay silently corrupt the allocator’s persistent metadata in a way that is difficult or impossible to recover from.
Sourcepub fn empty(allocator: &'a A) -> Self
pub fn empty(allocator: &'a A) -> Self
Construct a zero-length BStackSlice anchored at offset 0.
The resulting slice spans no bytes and all I/O methods on it are no-ops or return empty results. It is safe to construct because an empty slice cannot produce out-of-bounds reads or writes and carries no allocator-origin requirement.
Useful as a sentinel or default value when a slice field must be initialized before a real allocation is available.
Sourcepub fn to_bytes(&self) -> [u8; 16]
pub fn to_bytes(&self) -> [u8; 16]
Serialize this slice to a 16-byte array for on-disk storage.
Layout: offset as 8 bytes little-endian, then len as 8 bytes
little-endian. Reconstruct with BStackSlice::from_bytes.
Sourcepub unsafe fn from_bytes(allocator: &'a A, bytes: [u8; 16]) -> Self
pub unsafe fn from_bytes(allocator: &'a A, bytes: [u8; 16]) -> Self
Reconstruct a BStackSlice from a 16-byte array produced by
BStackSlice::to_bytes.
§Safety
The caller must ensure that bytes encodes a valid offset and length
that lie within the bounds of the underlying allocator’s payload.
Passing an arbitrary or corrupted byte array is undefined behaviour.
Sourcepub fn end(&self) -> u64
pub fn end(&self) -> u64
The exclusive end offset of this slice within the payload
(self.start() + self.len()).
Sourcepub fn range(&self) -> Range<u64>
pub fn range(&self) -> Range<u64>
Returns the range of this slice as start..end within the payload.
Sourcepub fn stack(&self) -> &BStack ⓘ
pub fn stack(&self) -> &BStack ⓘ
Return the underlying stack.
Note: Bstack does not require mutability for any of its operations,
and directly mutating the stack without the knowledge of the allocator
risks violating invariants. Therefore, use this method with caution
and prefer methods on BStackSlice such as read and
write that delegate to the stack internally.
Sourcepub fn subslice(&self, start: u64, end: u64) -> BStackSlice<'a, A>
pub fn subslice(&self, start: u64, end: u64) -> BStackSlice<'a, A>
Create a subslice of this slice.
Returns a new BStackSlice that refers to the subrange [start, end) within
this slice. The start and end parameters are relative to this slice’s start.
§Panics
Panics if start > end or end > self.len().
Sourcepub fn subslice_range(&self, range: Range<u64>) -> BStackSlice<'a, A>
pub fn subslice_range(&self, range: Range<u64>) -> BStackSlice<'a, A>
Create a subslice of this slice.
Returns a new BStackSlice that refers to the subrange range within
this slice. The range is relative to this slice’s start.
§Panics
Panics if range.start > range.end or range.end > self.len().
Sourcepub fn read(&self) -> Result<Vec<u8>>
pub fn read(&self) -> Result<Vec<u8>>
Read the entire slice into a newly allocated Vec<u8>.
Delegates to BStack::get.
§Errors
Returns an error if the range exceeds the current payload size.
Sourcepub fn read_into(&self, buf: &mut [u8]) -> Result<()>
pub fn read_into(&self, buf: &mut [u8]) -> Result<()>
Read bytes from this slice into the caller-supplied buf.
Reads min(buf.len(), self.len() as usize) bytes starting at
self.start(). If buf is shorter than the slice, only the first
buf.len() bytes are read. If buf is longer, only self.len() bytes
are filled and the remainder of buf is left untouched.
Sourcepub fn read_range(&self, start: u64, end: u64) -> Result<Vec<u8>>
pub fn read_range(&self, start: u64, end: u64) -> Result<Vec<u8>>
Read a sub-range [start, end) relative to this slice into a newly
allocated Vec<u8>.
start and end are relative to self.start(), not the payload start.
§Errors
Returns an error if start > end or if end exceeds self.len().
Sourcepub fn read_range_into(&self, start: u64, buf: &mut [u8]) -> Result<()>
pub fn read_range_into(&self, start: u64, buf: &mut [u8]) -> Result<()>
Read a sub-range [start, start + buf.len()) relative to this slice
into the caller-supplied buffer.
start is relative to self.start(), not the payload start.
§Errors
Returns io::ErrorKind::InvalidInput if start + buf.len() exceeds
self.len().
Sourcepub fn write(&self, data: impl AsRef<[u8]>) -> Result<()>
pub fn write(&self, data: impl AsRef<[u8]>) -> Result<()>
Overwrite the beginning of this slice in place with data.
Writes min(data.len(), self.len() as usize) bytes starting at
self.start(). If data is shorter than the slice, the remainder of
the slice is left untouched. If data is longer, only self.len()
bytes are written.
Requires the set feature.
Sourcepub fn write_range(&self, start: u64, data: impl AsRef<[u8]>) -> Result<()>
pub fn write_range(&self, start: u64, data: impl AsRef<[u8]>) -> Result<()>
Overwrite a sub-range [start, start + data.len()) within this slice
in place.
start is relative to self.start().
Requires the set feature.
§Errors
Returns io::ErrorKind::InvalidInput if start + data.len() exceeds
self.len().
Sourcepub fn zero_range(&self, start: u64, n: u64) -> Result<()>
pub fn zero_range(&self, start: u64, n: u64) -> Result<()>
Zero a sub-range [start, start + n) within this slice in place.
start is relative to self.start().
Requires the set feature.
§Errors
Returns io::ErrorKind::InvalidInput if start + n exceeds
self.len().
Sourcepub fn reader(&self) -> BStackSliceReader<'a, A> ⓘ
pub fn reader(&self) -> BStackSliceReader<'a, A> ⓘ
Sourcepub fn reader_at(&self, offset: u64) -> BStackSliceReader<'a, A> ⓘ
pub fn reader_at(&self, offset: u64) -> BStackSliceReader<'a, A> ⓘ
Create a cursor-based reader positioned at offset bytes into this slice.
offset is relative to self.start(). Seeking past self.len() is
allowed; subsequent reads return Ok(0).
Sourcepub fn writer(&self) -> BStackSliceWriter<'a, A> ⓘ
pub fn writer(&self) -> BStackSliceWriter<'a, A> ⓘ
Create a cursor-based writer positioned at the start of this slice.
Requires the set feature.
Sourcepub fn writer_at(&self, offset: u64) -> BStackSliceWriter<'a, A> ⓘ
pub fn writer_at(&self, offset: u64) -> BStackSliceWriter<'a, A> ⓘ
Create a cursor-based writer positioned at offset bytes into this slice.
offset is relative to self.start(). Writing past self.len()
returns Ok(0).
Requires the set feature.
Trait Implementations§
Source§impl<'a, A: BStackAllocator> Clone for BStackSlice<'a, A>
impl<'a, A: BStackAllocator> Clone for BStackSlice<'a, A>
Source§impl<'a, A: BStackAllocator> Debug for BStackSlice<'a, A>
impl<'a, A: BStackAllocator> Debug for BStackSlice<'a, A>
Source§impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for [u8; 16]
Serialize the slice to its 16-byte on-disk representation.
impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for [u8; 16]
Serialize the slice to its 16-byte on-disk representation.
Equivalent to BStackSlice::to_bytes.
Source§fn from(slice: BStackSlice<'a, A>) -> Self
fn from(slice: BStackSlice<'a, A>) -> Self
Source§impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
Convert a slice into a reader positioned at the start.
impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
Convert a slice into a reader positioned at the start.
Equivalent to BStackSlice::reader.
Source§fn from(slice: BStackSlice<'a, A>) -> Self
fn from(slice: BStackSlice<'a, A>) -> Self
Source§impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
Available on crate feature set only.Convert a slice into a writer positioned at the start.
impl<'a, A: BStackAllocator> From<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
set only.Convert a slice into a writer positioned at the start.
Equivalent to BStackSlice::writer.
Source§fn from(slice: BStackSlice<'a, A>) -> Self
fn from(slice: BStackSlice<'a, A>) -> Self
Source§impl<'a, A: BStackAllocator> From<BStackSliceReader<'a, A>> for BStackSlice<'a, A>
Convert a reader back into its underlying slice, discarding the cursor.
impl<'a, A: BStackAllocator> From<BStackSliceReader<'a, A>> for BStackSlice<'a, A>
Convert a reader back into its underlying slice, discarding the cursor.
Equivalent to BStackSliceReader::slice.
Source§fn from(reader: BStackSliceReader<'a, A>) -> Self
fn from(reader: BStackSliceReader<'a, A>) -> Self
Source§impl<'a, A: BStackAllocator> From<BStackSliceWriter<'a, A>> for BStackSlice<'a, A>
Available on crate feature set only.Convert a writer back into its underlying slice, discarding the cursor.
impl<'a, A: BStackAllocator> From<BStackSliceWriter<'a, A>> for BStackSlice<'a, A>
set only.Convert a writer back into its underlying slice, discarding the cursor.
Equivalent to BStackSliceWriter::slice.
Source§fn from(writer: BStackSliceWriter<'a, A>) -> Self
fn from(writer: BStackSliceWriter<'a, A>) -> Self
Source§impl<'a, A: BStackAllocator> Hash for BStackSlice<'a, A>
Hashes (offset, len), consistent with PartialEq.
impl<'a, A: BStackAllocator> Hash for BStackSlice<'a, A>
Hashes (offset, len), consistent with PartialEq.
Source§impl<'a, A: BStackAllocator> Ord for BStackSlice<'a, A>
Slices are ordered by start offset, then by length — consistent with Eq.
impl<'a, A: BStackAllocator> Ord for BStackSlice<'a, A>
Slices are ordered by start offset, then by length — consistent with Eq.
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a, A: BStackAllocator> PartialEq<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
impl<'a, A: BStackAllocator> PartialEq<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
Source§fn eq(&self, other: &BStackSlice<'a, A>) -> bool
fn eq(&self, other: &BStackSlice<'a, A>) -> bool
self and other values to be equal, and is used by ==.Source§impl<'a, A: BStackAllocator> PartialEq<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
Available on crate feature set only.
impl<'a, A: BStackAllocator> PartialEq<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
set only.Source§fn eq(&self, other: &BStackSlice<'a, A>) -> bool
fn eq(&self, other: &BStackSlice<'a, A>) -> bool
self and other values to be equal, and is used by ==.Source§impl<'a, A: BStackAllocator> PartialEq<BStackSliceReader<'a, A>> for BStackSlice<'a, A>
impl<'a, A: BStackAllocator> PartialEq<BStackSliceReader<'a, A>> for BStackSlice<'a, A>
Source§fn eq(&self, other: &BStackSliceReader<'a, A>) -> bool
fn eq(&self, other: &BStackSliceReader<'a, A>) -> bool
self and other values to be equal, and is used by ==.Source§impl<'a, A: BStackAllocator> PartialEq<BStackSliceWriter<'a, A>> for BStackSlice<'a, A>
Available on crate feature set only.
impl<'a, A: BStackAllocator> PartialEq<BStackSliceWriter<'a, A>> for BStackSlice<'a, A>
set only.Source§fn eq(&self, other: &BStackSliceWriter<'a, A>) -> bool
fn eq(&self, other: &BStackSliceWriter<'a, A>) -> bool
self and other values to be equal, and is used by ==.Source§impl<'a, A: BStackAllocator> PartialEq for BStackSlice<'a, A>
Two slices are equal when their offset and len match.
impl<'a, A: BStackAllocator> PartialEq for BStackSlice<'a, A>
Two slices are equal when their offset and len match.
Source§impl<'a, A: BStackAllocator> PartialOrd<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
impl<'a, A: BStackAllocator> PartialOrd<BStackSlice<'a, A>> for BStackSliceReader<'a, A>
Source§impl<'a, A: BStackAllocator> PartialOrd<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
Available on crate feature set only.
impl<'a, A: BStackAllocator> PartialOrd<BStackSlice<'a, A>> for BStackSliceWriter<'a, A>
set only.