soroban_sdk

Struct Bytes

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

Bytes is a contiguous growable array type containing u8s.

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

Source

pub fn env(&self) -> &Env

Source

pub fn as_val(&self) -> &Val

Source

pub fn to_val(&self) -> Val

Source

pub fn as_object(&self) -> &BytesObject

Source

pub fn to_object(&self) -> BytesObject

Source§

impl Bytes

Source

pub fn new(env: &Env) -> Bytes

Create an empty Bytes.

Source

pub fn from_array<const N: usize>(env: &Env, items: &[u8; N]) -> Bytes

Create a Bytes from the array.

Source

pub fn from_slice(env: &Env, items: &[u8]) -> Bytes

Create a Bytes from the slice.

Source

pub fn set(&mut self, i: u32, v: u8)

Sets the byte at the position with new value.

§Panics

If the position is out-of-bounds.

Source

pub fn get(&self, i: u32) -> Option<u8>

Returns the byte at the position or None if out-of-bounds.

Source

pub fn get_unchecked(&self, i: u32) -> u8

Returns the byte at the position.

§Panics

If the position is out-of-bounds.

Source

pub fn is_empty(&self) -> bool

Returns true if the Bytes is empty and has a length of zero.

Source

pub fn len(&self) -> u32

Returns the number of bytes are in the Bytes.

Source

pub fn first(&self) -> Option<u8>

Returns the first byte or None if empty.

Source

pub fn first_unchecked(&self) -> u8

Returns the first byte.

§Panics

If the Bytes is empty.

Source

pub fn last(&self) -> Option<u8>

Returns the last byte or None if empty.

Source

pub fn last_unchecked(&self) -> u8

Returns the last byte.

§Panics

If the Bytes is empty.

Source

pub fn remove(&mut self, i: u32) -> Option<()>

Removes the byte at the position.

Returns None if out-of-bounds.

Source

pub fn remove_unchecked(&mut self, i: u32)

Removes the byte at the position.

§Panics

If the position is out-of-bounds.

Source

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.

Source

pub fn pop_back(&mut self) -> Option<u8>

Removes and returns the last byte or None if empty.

Source

pub fn pop_back_unchecked(&mut self) -> u8

Removes and returns the last byte or None if empty.

§Panics

If the Bytes is empty.

Source

pub fn insert(&mut self, i: u32, b: u8)

Insert the byte at the position.

§Panics

If the position is out-of-bounds.

Source

pub fn insert_from_bytes(&mut self, i: u32, bytes: Bytes)

Insert the bytes at the position.

§Panics

If the position is out-of-bounds.

Source

pub fn insert_from_array<const N: usize>(&mut self, i: u32, array: &[u8; N])

Insert the bytes at the position.

§Panics

If the position is out-of-bounds.

Source

pub fn insert_from_slice(&mut self, i: u32, slice: &[u8])

Insert the bytes at the position.

§Panics

If the position is out-of-bounds.

Source

pub fn append(&mut self, other: &Bytes)

Append the bytes.

Source

pub fn extend_from_array<const N: usize>(&mut self, array: &[u8; N])

Extend with the bytes in the array.

Source

pub fn extend_from_slice(&mut self, slice: &[u8])

Extend with the bytes in the slice.

Source

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.

Source

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.

Source

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.

Source

pub fn iter(&self) -> BytesIter

Source

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.

Source

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<const N: usize> AsRef<Bytes> for BytesN<N>

Source§

fn as_ref(&self) -> &Bytes

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<const N: usize> Borrow<Bytes> for &BytesN<N>

Source§

fn borrow(&self) -> &Bytes

Immutably borrows from an owned value. Read more
Source§

impl<const N: usize> Borrow<Bytes> for &mut BytesN<N>

Source§

fn borrow(&self) -> &Bytes

Immutably borrows from an owned value. Read more
Source§

impl<const N: usize> Borrow<Bytes> for BytesN<N>

Source§

fn borrow(&self) -> &Bytes

Immutably borrows from an owned value. Read more
Source§

impl Clone for Bytes

Source§

fn clone(&self) -> Bytes

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bytes

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Fill for Bytes

Source§

fn fill(&mut self, prng: &Prng)

Fills the Bytes with the Prng.

§Panics

If the length of Bytes is greater than u32::MAX in length.

Source§

impl From<&Bytes> for Bytes

Source§

fn from(v: &Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<&Bytes> for BytesObject

Source§

fn from(v: &Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<&Bytes> for ScVal

Source§

fn from(v: &Bytes) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<&BytesN<N>> for Bytes

Source§

fn from(v: &BytesN<N>) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for BytesObject

Source§

fn from(v: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for ScVal

Source§

fn from(v: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Val

Source§

fn from(v: Bytes) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<BytesN<N>> for Bytes

Source§

fn from(v: BytesN<N>) -> Self

Converts to this type from the input type.
Source§

impl From<Fp> for Bytes

Source§

fn from(v: Fp) -> Self

Converts to this type from the input type.
Source§

impl From<Fp2> for Bytes

Source§

fn from(v: Fp2) -> Self

Converts to this type from the input type.
Source§

impl From<G1Affine> for Bytes

Source§

fn from(v: G1Affine) -> Self

Converts to this type from the input type.
Source§

impl From<G2Affine> for Bytes

Source§

fn from(v: G2Affine) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<Hash<N>> for Bytes

Source§

fn from(v: Hash<N>) -> Self

Converts to this type from the input type.
Source§

impl GenLen for Bytes

Source§

fn gen_len(prng: &Prng, len: u32) -> Self

Generates the Bytes with the Prng of the given length.

Source§

type Len = u32

Source§

impl IntoIterator for Bytes

Source§

type Item = u8

The type of the elements being iterated over.
Source§

type IntoIter = BytesIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for Bytes

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Bytes

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Bytes

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl SorobanArbitrary for Bytes

Available on crate feature testutils only.
Source§

type Prototype = ArbitraryBytes

A type that implements Arbitrary and can be converted to this SorobanArbitrary type.
Source§

impl<const N: usize> TryFrom<&Bytes> for [u8; N]

Source§

type Error = ConversionError

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

fn try_from(bin: &Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<&Bytes> for BytesN<N>

Source§

type Error = ConversionError

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

fn try_from(bin: &Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<Bytes> for [u8; N]

Source§

type Error = ConversionError

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

fn try_from(bin: Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<Bytes> for BytesN<N>

Source§

type Error = ConversionError

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

fn try_from(bin: Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFromVal<Env, &[u8]> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(env: &Env, v: &&[u8]) -> Result<Self, Self::Error>

Source§

impl TryFromVal<Env, &Bytes> for Val

Source§

type Error = ConversionError

Source§

fn try_from_val(_env: &Env, v: &&Bytes) -> Result<Self, Self::Error>

Source§

impl TryFromVal<Env, &str> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(env: &Env, v: &&str) -> Result<Self, Self::Error>

Source§

impl<const N: usize> TryFromVal<Env, [u8; N]> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(env: &Env, v: &[u8; N]) -> Result<Self, Self::Error>

Source§

impl TryFromVal<Env, Bytes> for Bytes

Source§

impl TryFromVal<Env, Bytes> for Val

Source§

impl<const N: usize> TryFromVal<Env, BytesN<N>> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(_env: &Env, v: &BytesN<N>) -> Result<Self, Self::Error>

Source§

impl TryFromVal<Env, BytesObject> for Bytes

Source§

impl TryFromVal<Env, ScVal> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, Self::Error>

Source§

impl TryFromVal<Env, Val> for Bytes

Source§

type Error = ConversionError

Source§

fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error>

Source§

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> 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T, C> Compare<&T> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare(&self, a: &&T, b: &&T) -> Result<Ordering, <C as Compare<&T>>::Error>

Source§

impl<T, U, E, C> Compare<(T, U)> for C
where C: Compare<T, Error = E, Error = E> + Compare<U>,

Source§

type Error = E

Source§

fn compare( &self, a: &(T, U), b: &(T, U), ) -> Result<Ordering, <C as Compare<(T, U)>>::Error>

Source§

impl<T, U, V, E, C> Compare<(T, U, V)> for C
where C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,

Source§

impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,

Source§

impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,

Source§

impl<T, C> Compare<Box<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Box<T>, b: &Box<T>, ) -> Result<Ordering, <C as Compare<Box<T>>>::Error>

Source§

impl<T, C> Compare<Option<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Option<T>, b: &Option<T>, ) -> Result<Ordering, <C as Compare<Option<T>>>::Error>

Source§

impl<T, C> Compare<Rc<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Rc<T>, b: &Rc<T>, ) -> Result<Ordering, <C as Compare<Rc<T>>>::Error>

Source§

impl<T, C> Compare<Vec<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Vec<T>, b: &Vec<T>, ) -> Result<Ordering, <C as Compare<Vec<T>>>::Error>

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<E, T, U> FromVal<E, T> for U
where E: Env, U: TryFromVal<E, T>,

Source§

fn from_val(e: &E, v: &T) -> U

Source§

impl<T> FromXdr for T
where T: TryFromVal<Env, Val>,

Source§

type Error = <T as TryFromVal<Env, Val>>::Error

Source§

fn from_xdr(env: &Env, b: &Bytes) -> Result<T, <T as FromXdr>::Error>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<E, T, U> IntoVal<E, T> for U
where E: Env, T: FromVal<E, U>,

Source§

fn into_val(&self, e: &E) -> T

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToXdr for T
where T: IntoVal<Env, Val>,

Source§

fn to_xdr(self, env: &Env) -> Bytes

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.
Source§

impl<E, T, U> TryIntoVal<E, T> for U
where E: Env, T: TryFromVal<E, U>,

Source§

type Error = <T as TryFromVal<E, U>>::Error

Source§

fn try_into_val(&self, env: &E) -> Result<T, <U as TryIntoVal<E, T>>::Error>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V