pub trait Encoding<const N: usize>where
Self: Sized,{
// Required methods
fn from_bytes(bytes: [u8; N]) -> Option<Self>;
fn to_bytes(&self) -> [u8; N];
// Provided methods
fn from_slice(slice: &[u8]) -> Option<Self> { ... }
fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()> { ... }
fn from_hex(hex: &str) -> Option<Self> { ... }
fn to_hex(&self) -> String { ... }
unsafe fn from_ptr(ptr: *const u8) -> Option<Self> { ... }
unsafe fn copy_to_ptr(self, ptr: *mut u8) { ... }
}Expand description
Adds encoding and decoding methods to PrivateKey, PublicKey, Triple, Scalar
and RistrettoPoint which can all be represented as [u8; N]s for some N.
Not all arrays of the form [u8; N] may be a valid representation of the type of object in question, though.
Required Methods§
Sourcefn from_bytes(bytes: [u8; N]) -> Option<Self>
fn from_bytes(bytes: [u8; N]) -> Option<Self>
Decodes Some(object) from bytes if bytes encodes some object of type Self;
otherwise returns None.
Provided Methods§
Sourcefn from_slice(slice: &[u8]) -> Option<Self>
fn from_slice(slice: &[u8]) -> Option<Self>
Like Self::from_bytes, but reads [u8; N] from slice. Returns None if slice.len()!=N
or when the slice is not a valid encoding.
Sourcefn copy_to_slice(&self, slice: &mut [u8]) -> Option<()>
fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()>
Copies the encoding of self into slice. Returns None when slice.len()!=N.
Sourcefn from_hex(hex: &str) -> Option<Self>
fn from_hex(hex: &str) -> Option<Self>
Like Self::from_bytes, but reads the [u8; N] from the 2*N-digit hex string hex.
The case of the hex digits is ignored.
Sourceunsafe fn from_ptr(ptr: *const u8) -> Option<Self>
unsafe fn from_ptr(ptr: *const u8) -> Option<Self>
Loads object from the N-byte buffer pointed to by ptr.
§Safety
The caller must make sure that ptr is properly alligned,
the N-byte buffer is readable, and isn’t modified for the duration of the call.
See the ‘Safety’ section of core::slice::from_raw_parts for more details.
Sourceunsafe fn copy_to_ptr(self, ptr: *mut u8)
unsafe fn copy_to_ptr(self, ptr: *mut u8)
Writes the N-byte representation of this object to the memory location ptr.
§Safety
The caller must make sure that ptr is properly alligned,
the N-byte buffer is writable, and isn’t modified for the duration of the call.
See the ‘Safety’ section of core::slice::from_raw_parts_mut for more details.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.