Skip to main content

ByteSlice

Struct ByteSlice 

Source
pub struct ByteSlice { /* private fields */ }
Expand description

A lightweight, clonable view into a subsection of a SharedBytes buffer.

This struct holds a reference-counted pointer to the underlying storage and a Range indicating the specific slice it represents. This makes it cheap to pass around, as it doesn’t own the actual data.

§Examples

ⓘ
use chaintools::io::storage::{ByteSlice, SharedBytes};

let storage = SharedBytes::from_owned(vec![0, 10, 20, 30, 40]);

// Create a slice representing the bytes 10, 20, 30
let byte_slice = ByteSlice::new(storage, 1..4);

assert_eq!(byte_slice.as_bytes(), &[10, 20, 30]);

// Clones are cheap
let another_slice = byte_slice.clone();
assert_eq!(another_slice.as_bytes(), &[10, 20, 30]);

Implementations§

Source§

impl ByteSlice

Source

pub fn new(storage: SharedBytes, range: Range<usize>) -> Self

Creates a new ByteSlice.

This is a lightweight operation, as it only clones the reference to the underlying storage and stores the range.

§Examples
ⓘ
use chaintools::io::storage::{ByteSlice, SharedBytes};

let storage = SharedBytes::from_owned(vec![0, 10, 20, 30, 40]);

// Create a slice representing the bytes 10, 20, 30
let byte_slice = ByteSlice::new(storage, 1..4);

assert_eq!(byte_slice.as_bytes(), &[10, 20, 30]);
Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Returns the byte slice represented by this ByteSlice.

This method provides a view into the portion of the underlying storage that this slice represents.

§Examples
ⓘ
use chaintools::io::storage::{ByteSlice, SharedBytes};

let storage = SharedBytes::from_owned(vec![0, 10, 20, 30, 40]);
let byte_slice = ByteSlice::new(storage, 1..4);

assert_eq!(byte_slice.as_bytes(), &[10, 20, 30]);
Source

pub fn as_str(&self) -> Option<&str>

Attempts to convert the byte slice to a UTF-8 string slice.

This will return Some(&str) if the slice contains valid UTF-8, and None otherwise.

§Examples
ⓘ
use chaintools::io::storage::{ByteSlice, SharedBytes};

// A slice with valid UTF-8
let storage1 = SharedBytes::from_owned(b"hello world".to_vec());
let slice1 = ByteSlice::new(storage1, 6..11);
assert_eq!(slice1.as_str(), Some("world"));

// A slice with invalid UTF-8
let storage2 = SharedBytes::from_owned(vec![0, 128, 255]);
let slice2 = ByteSlice::new(storage2, 0..3);
assert_eq!(slice2.as_str(), None);

Trait Implementations§

Source§

impl Clone for ByteSlice

Source§

fn clone(&self) -> ByteSlice

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 Debug for ByteSlice

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.