signature/
encoding.rs

1//! Encoding support.
2
3#[cfg(feature = "alloc")]
4use alloc::vec::Vec;
5
6/// Support for decoding/encoding signatures as bytes.
7pub trait SignatureEncoding:
8    Clone + Sized + for<'a> TryFrom<&'a [u8]> + TryInto<Self::Repr>
9{
10    /// Byte representation of a signature.
11    type Repr: 'static + AsRef<[u8]> + Clone + Send + Sync;
12
13    /// Encode signature as its byte representation.
14    fn to_bytes(&self) -> Self::Repr {
15        self.clone()
16            .try_into()
17            .ok()
18            .expect("signature encoding error")
19    }
20
21    /// Encode signature as a byte vector.
22    #[cfg(feature = "alloc")]
23    fn to_vec(&self) -> Vec<u8> {
24        self.to_bytes().as_ref().to_vec()
25    }
26
27    /// Get the length of this signature when encoded.
28    fn encoded_len(&self) -> usize {
29        self.to_bytes().as_ref().len()
30    }
31}