From 712ccb629d64552561e2eeb1a1bf094f65c7f3ea Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 13 Nov 2023 22:06:25 +0100 Subject: [PATCH] Add const instantiation functions for byte types (#380) --- bindings/rust/src/bindings/mod.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index 2b0cfad..ebeee8a 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -218,6 +218,11 @@ impl Drop for KZGSettings { } 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 { if bytes.len() != BYTES_PER_BLOB { return Err(Error::InvalidBytesLength(format!( @@ -228,7 +233,7 @@ impl Blob { } let mut new_bytes = [0; BYTES_PER_BLOB]; new_bytes.copy_from_slice(bytes); - Ok(Self { bytes: new_bytes }) + Ok(Self::new(new_bytes)) } pub fn from_hex(hex_str: &str) -> Result { @@ -243,6 +248,11 @@ impl AsRef<[u8]> for Blob { } 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 { if bytes.len() != 32 { return Err(Error::InvalidBytesLength(format!( @@ -253,7 +263,7 @@ impl Bytes32 { } let mut new_bytes = [0; 32]; new_bytes.copy_from_slice(bytes); - Ok(Self { bytes: new_bytes }) + Ok(Self::new(new_bytes)) } pub fn from_hex(hex_str: &str) -> Result { @@ -262,6 +272,11 @@ impl Bytes32 { } 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 { if bytes.len() != 48 { return Err(Error::InvalidBytesLength(format!( @@ -272,7 +287,7 @@ impl Bytes48 { } let mut new_bytes = [0; 48]; new_bytes.copy_from_slice(bytes); - Ok(Self { bytes: new_bytes }) + Ok(Self::new(new_bytes)) } pub fn from_hex(hex_str: &str) -> Result {