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
impl ByteSlice
Sourcepub fn new(storage: SharedBytes, range: Range<usize>) -> Self
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]);Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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]);Sourcepub fn as_str(&self) -> Option<&str>
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§
Auto Trait Implementations§
impl Freeze for ByteSlice
impl RefUnwindSafe for ByteSlice
impl Send for ByteSlice
impl Sync for ByteSlice
impl Unpin for ByteSlice
impl UnsafeUnpin for ByteSlice
impl UnwindSafe for ByteSlice
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more