Skip to main content

BoundedString

Struct BoundedString 

Source
pub struct BoundedString<const N: usize> { /* private fields */ }
Expand description

Bounded UTF-8 string for Hopper dynamic tails.

This is the common migration target for bounded string account metadata. It keeps a fixed [u8; N] backing buffer, carries a small length prefix on the tail wire, and validates UTF-8 when read as &str.

Implementations§

Source§

impl<const N: usize> BoundedString<N>

Source

pub const fn empty() -> BoundedString<N>

Construct an empty bounded string.

Source

pub fn from_str(value: &str) -> Result<BoundedString<N>, ProgramError>

Construct from UTF-8 bytes, rejecting values longer than N.

Source

pub fn from_bytes(value: &[u8]) -> Result<BoundedString<N>, ProgramError>

Construct from bytes, rejecting values longer than N.

Source

pub fn set_str(&mut self, value: &str) -> Result<(), ProgramError>

Replace the contents in place.

Source

pub fn set_bytes(&mut self, value: &[u8]) -> Result<(), ProgramError>

Replace the contents in place.

Source

pub fn clear(&mut self)

Clear the string without changing its capacity.

Source

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

Return the initialized bytes.

Source

pub fn as_str(&self) -> Result<&str, ProgramError>

Return the initialized bytes as UTF-8.

Source

pub const fn len(&self) -> usize

Number of initialized bytes.

Source

pub const fn capacity(&self) -> usize

Maximum byte capacity.

Source

pub const fn remaining_capacity(&self) -> usize

Remaining byte capacity.

Source

pub const fn is_full(&self) -> bool

Whether the string has reached its maximum byte capacity.

Source

pub const fn is_empty(&self) -> bool

Whether the string is empty.

Trait Implementations§

Source§

impl<const N: usize> Clone for BoundedString<N>

Source§

fn clone(&self) -> BoundedString<N>

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<const N: usize> Default for BoundedString<N>

Source§

fn default() -> BoundedString<N>

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> PartialEq for BoundedString<N>

Source§

fn eq(&self, other: &BoundedString<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<const N: usize> TailCodec for BoundedString<N>

Source§

const MAX_ENCODED_LEN: usize

Upper bound on the encoded size. Used by generated helpers to verify the account has enough room before invoking encode. Implementors should pick the smallest valid bound. Hopper uses this to pre-size reallocs.
Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Serialize self into out. Returns the number of bytes written (always <= MAX_ENCODED_LEN). Fails with AccountDataTooSmall when out.len() < encoded_len.
Source§

fn decode(input: &[u8]) -> Result<(BoundedString<N>, usize), ProgramError>

Deserialize from input. Returns (value, bytes_consumed). Fails with InvalidAccountData on malformed encoding.
Source§

impl<const N: usize> Copy for BoundedString<N>

Source§

impl<const N: usize> Eq for BoundedString<N>

Source§

impl<const N: usize> StructuralPartialEq for BoundedString<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for BoundedString<N>

§

impl<const N: usize> RefUnwindSafe for BoundedString<N>

§

impl<const N: usize> Send for BoundedString<N>

§

impl<const N: usize> Sync for BoundedString<N>

§

impl<const N: usize> Unpin for BoundedString<N>

§

impl<const N: usize> UnsafeUnpin for BoundedString<N>

§

impl<const N: usize> UnwindSafe for BoundedString<N>

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

impl<T> TailElement for T