pub struct Nonce(/* private fields */);Implementations§
Source§impl Nonce
impl Nonce
Sourcepub const fn new(nonce: [u8; 32]) -> Self
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);Sourcepub const fn from_short_str(s: &str) -> Self
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");Sourcepub fn as_bytes(&self) -> &[u8; 32]
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 operationsTrait Implementations§
Source§impl BorshDeserialize for Nonce
impl BorshDeserialize for Nonce
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl BorshSerialize for Nonce
impl BorshSerialize for Nonce
impl Copy for Nonce
Source§impl<'de> Deserialize<'de> for Nonce
impl<'de> Deserialize<'de> for Nonce
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Nonce
impl Display for Nonce
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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 representationimpl Eq for Nonce
Source§impl<const N: usize> From<&[u8; N]> for Nonce
impl<const N: usize> From<&[u8; N]> for Nonce
Source§fn from(nonce: &[u8; N]) -> Self
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 aNONCE_LEN-byte nonce - If
N <= NONCE_LEN: The input is padded with zeros to reachNONCE_LENbytes
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
impl From<&[u8]> for Nonce
Source§fn from(nonce: &[u8]) -> Self
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_LENbytes: 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
impl From<&str> for Nonce
Source§fn from(nonce: &str) -> Self
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<const N: usize> From<[u8; N]> for Nonce
impl<const N: usize> From<[u8; N]> for Nonce
Source§fn from(nonce: [u8; N]) -> Self
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
impl From<u64> for Nonce
Source§fn from(nonce: u64) -> Self
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 Ord for Nonce
impl Ord for Nonce
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Nonce
impl PartialOrd for Nonce
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> 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
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.