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

Conversation

xgreenx
Copy link
Collaborator

@xgreenx xgreenx commented Sep 26, 2024

Implemented AsRef<[u8]> and TryFrom<&[u8]> for DA compression types: ScriptCode, PredicateCode, RegistryKey. It allows to use Raw codec in the fuel-core

Before requesting review

  • I have reviewed the code myself

@xgreenx xgreenx requested a review from a team September 26, 2024 18:03
@xgreenx xgreenx self-assigned this Sep 26, 2024
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

LGTM. Just one nit suggestion.

Comment on lines +57 to +69
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))
}
}
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;

@xgreenx xgreenx added this pull request to the merge queue Sep 27, 2024
Merged via the queue into master with commit 1e7560b Sep 27, 2024
39 checks passed
@xgreenx xgreenx deleted the feature/raw-codec-for-new-types branch September 27, 2024 07:00
@xgreenx xgreenx mentioned this pull request Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants