Struct soroban_sdk::Bytes
source · 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])
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
Trait Implementations§
source§impl IntoIterator for Bytes
impl IntoIterator for Bytes
source§impl Ord for Bytes
impl Ord for Bytes
source§impl PartialEq for Bytes
impl PartialEq for Bytes
source§impl PartialOrd for Bytes
impl PartialOrd for Bytes
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SorobanArbitrary for Bytes
Available on crate feature testutils
only.
impl SorobanArbitrary for Bytes
testutils
only.§type Prototype = ArbitraryBytes
type Prototype = ArbitraryBytes
Arbitrary
and can be converted to this
SorobanArbitrary
type.source§impl TryFrom<&Bytes> for ScVal
impl TryFrom<&Bytes> for ScVal
§type Error = ConversionError
type Error = ConversionError
source§impl TryFrom<Bytes> for ScVal
impl TryFrom<Bytes> for ScVal
§type Error = ConversionError
type Error = ConversionError
source§impl TryFromVal<Env, &[u8]> for Bytes
impl TryFromVal<Env, &[u8]> for Bytes
type Error = ConversionError
fn try_from_val(env: &Env, v: &&[u8]) -> Result<Self, Self::Error>
source§impl TryFromVal<Env, &str> for Bytes
impl TryFromVal<Env, &str> for Bytes
type Error = ConversionError
fn try_from_val(env: &Env, v: &&str) -> Result<Self, Self::Error>
source§impl TryFromVal<Env, Bytes> for Bytes
impl TryFromVal<Env, Bytes> for Bytes
type Error = ConversionError
fn try_from_val(_env: &Env, v: &Bytes) -> Result<Self, Self::Error>
source§impl TryFromVal<Env, Bytes> for Val
impl TryFromVal<Env, Bytes> for Val
type Error = ConversionError
fn try_from_val(_env: &Env, v: &Bytes) -> Result<Self, Self::Error>
source§impl<const N: usize> TryFromVal<Env, BytesN<N>> for Bytes
impl<const N: usize> TryFromVal<Env, BytesN<N>> for Bytes
type Error = ConversionError
fn try_from_val(_env: &Env, v: &BytesN<N>) -> Result<Self, Self::Error>
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>
source§impl TryFromVal<Env, ScVal> for Bytes
impl TryFromVal<Env, ScVal> for Bytes
type Error = ConversionError
fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, Self::Error>
source§impl TryFromVal<Env, Val> for Bytes
impl TryFromVal<Env, Val> for Bytes
type Error = ConversionError
fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error>
impl Eq for Bytes
Auto Trait Implementations§
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
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
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>
§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
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>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§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
.§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
.§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.§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.