pub struct Bytes { /* private fields */ }
Expand description
Bytes is a contiguous growable array type containing u8
s.
The array is stored in the Host and available to the Guest through the functions defined on Bytes.
Bytes values can be stored as Storage, or in other types like Vec, Map, etc.
§Examples
Bytes values can be created from slices:
use soroban_sdk::{Bytes, Env};
let env = Env::default();
let bytes = Bytes::from_slice(&env, &[1; 32]);
assert_eq!(bytes.len(), 32);
let mut slice = [0u8; 32];
bytes.copy_into_slice(&mut slice);
assert_eq!(slice, [1u8; 32]);
Implementations§
Source§impl Bytes
impl Bytes
Sourcepub fn from_array<const N: usize>(env: &Env, items: &[u8; N]) -> Bytes
pub fn from_array<const N: usize>(env: &Env, items: &[u8; N]) -> Bytes
Create a Bytes from the array.
Sourcepub fn from_slice(env: &Env, items: &[u8]) -> Bytes
pub fn from_slice(env: &Env, items: &[u8]) -> Bytes
Create a Bytes from the slice.
Sourcepub fn get(&self, i: u32) -> Option<u8>
pub fn get(&self, i: u32) -> Option<u8>
Returns the byte at the position or None if out-of-bounds.
Sourcepub fn get_unchecked(&self, i: u32) -> u8
pub fn get_unchecked(&self, i: u32) -> u8
Sourcepub fn first_unchecked(&self) -> u8
pub fn first_unchecked(&self) -> u8
Sourcepub fn last_unchecked(&self) -> u8
pub fn last_unchecked(&self) -> u8
Sourcepub fn remove(&mut self, i: u32) -> Option<()>
pub fn remove(&mut self, i: u32) -> Option<()>
Removes the byte at the position.
Returns None
if out-of-bounds.
Sourcepub fn remove_unchecked(&mut self, i: u32)
pub fn remove_unchecked(&mut self, i: u32)
Sourcepub fn push_back(&mut self, x: u8)
pub fn push_back(&mut self, x: u8)
Adds the byte to the back.
Increases the length by one and puts the byte in the last position.
Sourcepub fn pop_back_unchecked(&mut self) -> u8
pub fn pop_back_unchecked(&mut self) -> u8
Sourcepub fn insert_from_bytes(&mut self, i: u32, bytes: Bytes)
pub fn insert_from_bytes(&mut self, i: u32, bytes: Bytes)
Sourcepub fn insert_from_slice(&mut self, i: u32, slice: &[u8])
pub fn insert_from_slice(&mut self, i: u32, slice: &[u8])
Sourcepub fn extend_from_array<const N: usize>(&mut self, array: &[u8; N])
pub fn extend_from_array<const N: usize>(&mut self, array: &[u8; N])
Extend with the bytes in the array.
Sourcepub fn extend_from_slice(&mut self, slice: &[u8])
pub fn extend_from_slice(&mut self, slice: &[u8])
Extend with the bytes in the slice.
Sourcepub fn copy_from_slice(&mut self, i: u32, slice: &[u8])
pub fn copy_from_slice(&mut self, i: u32, slice: &[u8])
Copy the bytes from slice.
The full number of bytes in slice are always copied and Bytes is grown if necessary.
Sourcepub fn copy_into_slice(&self, slice: &mut [u8])
pub fn copy_into_slice(&self, slice: &mut [u8])
Copy the bytes into the given slice.
§Panics
If the output slice and bytes are of different lengths.
Sourcepub fn slice(&self, r: impl RangeBounds<u32>) -> Self
pub fn slice(&self, r: impl RangeBounds<u32>) -> Self
Returns a subset of the bytes as defined by the start and end bounds of the range.
§Panics
If the range is out-of-bounds.
pub fn iter(&self) -> BytesIter
Sourcepub fn to_buffer<const B: usize>(&self) -> BytesBuffer<B>
pub fn to_buffer<const B: usize>(&self) -> BytesBuffer<B>
Copy the bytes into a buffer of given size.
Returns the buffer and a range of where the bytes live in the given buffer.
Suitable when the size of the bytes isn’t a fixed size but it is known to be under a certain size, or failure due to overflow is acceptable.
§Panics
If the size of the bytes is larger than the size of the buffer. To avoid this, first slice the bytes into a smaller size then convert to a buffer.
Sourcepub fn to_alloc_vec(&self) -> Vec<u8> ⓘ
pub fn to_alloc_vec(&self) -> Vec<u8> ⓘ
Copy the bytes into a Rust alloc Vec of size matching the bytes.
Returns the Vec. Allocates using the built-in allocator.
Suitable when the size of the bytes isn’t a fixed size and the allocator functionality of the sdk is enabled.
Trait Implementations§
Source§impl From<&Bytes> for BytesObject
impl From<&Bytes> for BytesObject
Source§impl From<Bytes> for BytesObject
impl From<Bytes> for BytesObject
Source§impl IntoIterator for Bytes
impl IntoIterator for Bytes
Source§impl Ord for Bytes
impl Ord for Bytes
Source§impl PartialOrd for Bytes
impl PartialOrd for Bytes
Source§impl SorobanArbitrary for Bytes
Available on crate feature testutils
only.
impl SorobanArbitrary for Bytes
testutils
only.Source§type Prototype = ArbitraryBytes
type Prototype = ArbitraryBytes
Arbitrary
and can be converted to this
SorobanArbitrary
type.Source§impl TryFromVal<Env, BytesObject> for Bytes
impl TryFromVal<Env, BytesObject> for Bytes
type Error = Infallible
fn try_from_val(env: &Env, val: &BytesObject) -> Result<Self, Self::Error>
impl Eq for Bytes
Auto Trait Implementations§
impl Freeze for Bytes
impl !RefUnwindSafe for Bytes
impl !Send for Bytes
impl !Sync for Bytes
impl Unpin for Bytes
impl !UnwindSafe for Bytes
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more