Add const instantiation functions for byte types (#380)
This commit is contained in:
parent
29f4a6807f
commit
712ccb629d
|
@ -218,6 +218,11 @@ impl Drop for KZGSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Blob {
|
impl Blob {
|
||||||
|
/// Creates a new blob from a byte array.
|
||||||
|
pub const fn new(bytes: [u8; BYTES_PER_BLOB]) -> Self {
|
||||||
|
Self { bytes }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
if bytes.len() != BYTES_PER_BLOB {
|
if bytes.len() != BYTES_PER_BLOB {
|
||||||
return Err(Error::InvalidBytesLength(format!(
|
return Err(Error::InvalidBytesLength(format!(
|
||||||
|
@ -228,7 +233,7 @@ impl Blob {
|
||||||
}
|
}
|
||||||
let mut new_bytes = [0; BYTES_PER_BLOB];
|
let mut new_bytes = [0; BYTES_PER_BLOB];
|
||||||
new_bytes.copy_from_slice(bytes);
|
new_bytes.copy_from_slice(bytes);
|
||||||
Ok(Self { bytes: new_bytes })
|
Ok(Self::new(new_bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
||||||
|
@ -243,6 +248,11 @@ impl AsRef<[u8]> for Blob {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bytes32 {
|
impl Bytes32 {
|
||||||
|
/// Creates a new instance from a byte array.
|
||||||
|
pub const fn new(bytes: [u8; 32]) -> Self {
|
||||||
|
Self { bytes }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
if bytes.len() != 32 {
|
if bytes.len() != 32 {
|
||||||
return Err(Error::InvalidBytesLength(format!(
|
return Err(Error::InvalidBytesLength(format!(
|
||||||
|
@ -253,7 +263,7 @@ impl Bytes32 {
|
||||||
}
|
}
|
||||||
let mut new_bytes = [0; 32];
|
let mut new_bytes = [0; 32];
|
||||||
new_bytes.copy_from_slice(bytes);
|
new_bytes.copy_from_slice(bytes);
|
||||||
Ok(Self { bytes: new_bytes })
|
Ok(Self::new(new_bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
||||||
|
@ -262,6 +272,11 @@ impl Bytes32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bytes48 {
|
impl Bytes48 {
|
||||||
|
/// Creates a new instance from a byte array.
|
||||||
|
pub const fn new(bytes: [u8; 48]) -> Self {
|
||||||
|
Self { bytes }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
if bytes.len() != 48 {
|
if bytes.len() != 48 {
|
||||||
return Err(Error::InvalidBytesLength(format!(
|
return Err(Error::InvalidBytesLength(format!(
|
||||||
|
@ -272,7 +287,7 @@ impl Bytes48 {
|
||||||
}
|
}
|
||||||
let mut new_bytes = [0; 48];
|
let mut new_bytes = [0; 48];
|
||||||
new_bytes.copy_from_slice(bytes);
|
new_bytes.copy_from_slice(bytes);
|
||||||
Ok(Self { bytes: new_bytes })
|
Ok(Self::new(new_bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
|
||||||
|
|
Loading…
Reference in New Issue