Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing new functions for byte types #380

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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<Self, Error> {
if bytes.len() != BYTES_PER_BLOB {
return Err(Error::InvalidBytesLength(format!(
Expand All @@ -252,7 +257,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<Self, Error> {
Expand All @@ -261,6 +266,11 @@ impl 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<Self, Error> {
if bytes.len() != 32 {
return Err(Error::InvalidBytesLength(format!(
Expand All @@ -271,7 +281,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<Self, Error> {
Expand All @@ -280,6 +290,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<Self, Error> {
if bytes.len() != 48 {
return Err(Error::InvalidBytesLength(format!(
Expand All @@ -290,7 +305,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<Self, Error> {
Expand Down