Skip to main content

Nonce

Struct Nonce 

Source
pub struct Nonce(/* private fields */);

Implementations§

Source§

impl Nonce

Source

pub const fn new(nonce: [u8; 32]) -> Self

Creates a new Nonce directly from a NONCE_LEN-byte array.

This is the most direct way to create a Nonce when you already have a properly sized NONCE_LEN-byte array. No padding or hashing is performed.

§Examples
let bytes = [1u8; 32];
let nonce = Nonce::new(bytes);
assert_eq!(nonce.as_bytes(), &bytes);
Source

pub const fn from_short_str(s: &str) -> Self

Creates a Nonce from a string slice at compile time.

If the string is shorter than NONCE_LEN bytes, it is padded with zeros. If the string is longer than NONCE_LEN bytes, it is truncated to NONCE_LEN bytes.

Note: This behavior matches From<&str> for strings up to NONCE_LEN bytes. For longer strings, From<&str> would perform a Blake3 hash instead of truncation.

§Examples
const CONNECT: Nonce = Nonce::from_short_str("connect");
Source

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

Returns a reference to the inner NONCE_LEN-byte array.

This method is useful when you need to access the raw bytes of the nonce, for example when using it as a seed for PDA derivation.

§Examples
let nonce = Nonce::from("example");
let bytes = nonce.as_bytes();
// Use bytes for PDA derivation or other operations

Trait Implementations§

Source§

impl BorshDeserialize for Nonce

Source§

fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

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

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl BorshSerialize for Nonce

Source§

fn serialize<__W: Write>(&self, writer: &mut __W) -> Result<(), Error>

Source§

impl Clone for Nonce

Source§

fn clone(&self) -> Nonce

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 Copy for Nonce

Source§

impl Debug for Nonce

Source§

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

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

impl Default for Nonce

Source§

fn default() -> Nonce

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

impl<'de> Deserialize<'de> for Nonce

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Nonce

Source§

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

Formats the Nonce as a hexadecimal string.

This implementation converts the NONCE_LEN-byte nonce to a 64-character hexadecimal string representation, which is useful for logging, debugging, and serialization to human-readable formats.

§Examples
let nonce = Nonce::from([1u8; 32]);
let hex_string = nonce.to_string();
println!("Nonce: {}", nonce); // Prints the hex representation
Source§

impl Eq for Nonce

Source§

impl From<&String> for Nonce

Source§

fn from(nonce: &String) -> Self

Creates a Nonce from a reference to a String.

This implementation is similar to the owned String implementation, converting the string to bytes and applying the standard rules.

§Examples
let nonce = Nonce::from(&String::from("account:user123"));
Source§

impl From<&Vec<u8>> for Nonce

Source§

fn from(nonce: &Vec<u8>) -> Self

Creates a Nonce from a reference to a vector of bytes.

This implementation converts the vector to a slice and applies the same padding/hashing rules as the slice implementation.

§Examples
let vec = vec![1, 2, 3, 4];
let nonce = Nonce::from(vec.as_slice());
Source§

impl<const N: usize> From<&[u8; N]> for Nonce

Source§

fn from(nonce: &[u8; N]) -> Self

Creates a Nonce from a reference to a fixed-size byte array of any length.

§Behavior
  • If N > NONCE_LEN: The input is hashed using Blake3 to produce a NONCE_LEN-byte nonce
  • If N <= NONCE_LEN: The input is padded with zeros to reach NONCE_LEN bytes

The padding is applied to the right side, preserving the original bytes at the beginning.

§Examples
// Small array (padded)
let small = [1, 2, 3, 4];
let nonce_small = Nonce::from(&small);

// Large array (hashed)
let large = [1u8; 64];
let nonce_large = Nonce::from(&large);
Source§

impl From<&[u8]> for Nonce

Source§

fn from(nonce: &[u8]) -> Self

Creates a Nonce from a byte slice of any length.

This implementation handles dynamic-length byte slices, applying the same logic as the fixed-size array implementation:

§Behavior
  • If nonce.len() > NONCE_LEN: The input is hashed using Blake3
  • If nonce.len() <= NONCE_LEN: The input is padded with zeros
§Edge Cases
  • Empty slice: Results in a nonce of all zeros
  • Exactly NONCE_LEN bytes: No transformation is applied, but the bytes are copied
§Examples
// Empty slice
let empty: &[u8] = &[];
let nonce_empty = Nonce::from(empty);
assert_eq!(*nonce_empty.as_bytes(), [0u8; 32]);

// Dynamic slice
let data = b"dynamic length data".to_vec();
let nonce_dynamic = Nonce::from(data.as_slice());
Source§

impl From<&str> for Nonce

Source§

fn from(nonce: &str) -> Self

Creates a Nonce from a string slice.

This implementation converts the string to bytes and then applies the standard padding/hashing rules. This is particularly useful for creating deterministic nonces from human-readable identifiers.

§Examples
let nonce = Nonce::from("account:user123");
§Note

String-based nonces are convenient but be aware that:

  • Different string encodings could produce different nonces
  • Unicode strings may have unexpected byte representations
Source§

impl From<String> for Nonce

Source§

fn from(nonce: String) -> Self

Creates a Nonce from an owned String.

This implementation converts the String to bytes and applies the same padding/hashing rules as the string slice implementation.

§Examples
let nonce = Nonce::from(String::from("account:user123"));
Source§

impl From<Vec<u8>> for Nonce

Source§

fn from(nonce: Vec<u8>) -> Self

Creates a Nonce from an owned vector of bytes.

This implementation converts the vector to a slice and applies the same padding/hashing rules as the slice implementation.

§Examples
let vec = vec![1, 2, 3, 4];
let nonce = Nonce::from(vec);
Source§

impl<const N: usize> From<[u8; N]> for Nonce

Source§

fn from(nonce: [u8; N]) -> Self

Creates a Nonce from an owned fixed-size byte array.

This implementation delegates to the reference implementation to avoid code duplication. The same padding or hashing rules apply as in the reference implementation.

§Examples
let array = [5u8; 16];
let nonce = Nonce::from(array);
Source§

impl From<u64> for Nonce

Source§

fn from(nonce: u64) -> Self

Creates a Nonce from a u64 integer.

The integer is converted to bytes in little-endian format before being padded to NONCE_LEN bytes. This is useful for creating sequential or indexed nonces.

§Examples
// Create sequential nonces
let nonce1 = Nonce::from(1u64);
let nonce2 = Nonce::from(2u64);
§Note

Since u64 is 8 bytes, the resulting nonce will always be padded rather than hashed. The first 8 bytes will contain the little-endian representation of the integer, and the remaining 24 bytes will be zeros.

Source§

impl Hash for Nonce

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Nonce

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · 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 Nonce

Source§

fn eq(&self, other: &Nonce) -> 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 PartialOrd for Nonce

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Serialize for Nonce

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Nonce

Auto Trait Implementations§

§

impl Freeze for Nonce

§

impl RefUnwindSafe for Nonce

§

impl Send for Nonce

§

impl Sync for Nonce

§

impl Unpin for Nonce

§

impl UnsafeUnpin for Nonce

§

impl UnwindSafe for Nonce

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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRexValueFor<T> for T
where T: BorshSerialize,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more