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

Implemented AsRef<[u8]> and TryFrom<&[u8]> for DA compression types #838

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [#838](https://github.com/FuelLabs/fuel-vm/pull/838): Implemented `AsRef<[u8]>` and `TryFrom<&[u8]>` for DA compression types: ScriptCode, PredicateCode, RegistryKey.

### Fixed
- [#835](https://github.com/FuelLabs/fuel-vm/pull/835): Fixing WASM-NPM packaging and publishing

Expand Down
21 changes: 21 additions & 0 deletions fuel-compression/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl RegistryKey {
}
}
}

impl TryFrom<u32> for RegistryKey {
type Error = &'static str;

Expand All @@ -53,6 +54,26 @@ impl TryFrom<u32> for RegistryKey {
}
}

impl TryFrom<&[u8]> for RegistryKey {
type Error = &'static str;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != Self::SIZE {
return Err("RegistryKey must be 3 bytes long");
}

let mut bytes = [0u8; 3];
bytes.copy_from_slice(value);
Ok(Self(bytes))
}
}
Comment on lines +57 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: How about using a dedicated error type here?

Suggested change
impl TryFrom<&[u8]> for RegistryKey {
type Error = &'static str;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != Self::SIZE {
return Err("RegistryKey must be 3 bytes long");
}
let mut bytes = [0u8; 3];
bytes.copy_from_slice(value);
Ok(Self(bytes))
}
}
impl TryFrom<&[u8]> for RegistryKey {
type Error = InvalidRegistryKeySize;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != Self::SIZE {
return Err(InvalidRegistryKeySize);
}
let mut bytes = [0u8; 3];
bytes.copy_from_slice(value);
Ok(Self(bytes))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::Display, derive_more::Error)]
#[display("RegistryKey must be 3 bytes long")]
struct InvalidRegistryKeySize;


impl AsRef<[u8]> for RegistryKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::RegistryKey;
Expand Down
23 changes: 23 additions & 0 deletions fuel-tx/src/transaction/types/input/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,41 @@ pub struct PredicateCode {
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
pub bytes: Vec<u8>,
}

impl From<Vec<u8>> for PredicateCode {
fn from(bytes: Vec<u8>) -> Self {
Self { bytes }
}
}

impl From<&[u8]> for PredicateCode {
fn from(bytes: &[u8]) -> Self {
Self {
bytes: bytes.to_vec(),
}
}
}

impl AsRef<[u8]> for PredicateCode {
fn as_ref(&self) -> &[u8] {
&self.bytes
}
}

impl AsMut<[u8]> for PredicateCode {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.bytes
}
}

impl Deref for PredicateCode {
type Target = Vec<u8>;

fn deref(&self) -> &Self::Target {
&self.bytes
}
}

impl DerefMut for PredicateCode {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.bytes
Expand Down
23 changes: 23 additions & 0 deletions fuel-tx/src/transaction/types/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,41 @@ pub struct ScriptCode {
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
pub bytes: Vec<u8>,
}

impl From<Vec<u8>> for ScriptCode {
fn from(bytes: Vec<u8>) -> Self {
Self { bytes }
}
}

impl From<&[u8]> for ScriptCode {
fn from(bytes: &[u8]) -> Self {
Self {
bytes: bytes.to_vec(),
}
}
}

impl AsRef<[u8]> for ScriptCode {
fn as_ref(&self) -> &[u8] {
&self.bytes
}
}

impl AsMut<[u8]> for ScriptCode {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.bytes
}
}

impl Deref for ScriptCode {
type Target = Vec<u8>;

fn deref(&self) -> &Self::Target {
&self.bytes
}
}

impl DerefMut for ScriptCode {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.bytes
Expand Down
Loading