Skip to content

Commit

Permalink
Implemented AsRef<[u8]> and TryFrom<&[u8]> for DA compression typ…
Browse files Browse the repository at this point in the history
…es (#838)

* Implemented `AsRef<[u8]>` and `TryFrom<&[u8]>` for DA compression types

* Updated CHANGELOG.md
  • Loading branch information
xgreenx authored Sep 27, 2024
1 parent dffc2b4 commit 1e7560b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
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))
}
}

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

0 comments on commit 1e7560b

Please sign in to comment.