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

Add AsRef and AsMut for AccountId20 #1139

Merged
merged 2 commits into from
Aug 1, 2023
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
37 changes: 37 additions & 0 deletions primitives/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ impl From<[u8; 20]> for AccountId20 {
}
}

impl<'a> TryFrom<&'a [u8]> for AccountId20 {
type Error = ();
fn try_from(x: &'a [u8]) -> Result<AccountId20, ()> {
if x.len() == 20 {
let mut data = [0; 20];
data.copy_from_slice(x);
Ok(AccountId20(data))
} else {
Err(())
}
}
}

impl From<AccountId20> for [u8; 20] {
fn from(val: AccountId20) -> Self {
val.0
Expand All @@ -102,6 +115,30 @@ impl From<AccountId20> for H160 {
}
}

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

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

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

impl AsMut<[u8; 20]> for AccountId20 {
fn as_mut(&mut self) -> &mut [u8; 20] {
&mut self.0
}
}

impl From<ecdsa::Public> for AccountId20 {
fn from(pk: ecdsa::Public) -> Self {
let decompressed = libsecp256k1::PublicKey::parse_compressed(&pk.0)
Expand Down
11 changes: 4 additions & 7 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[toolchain]
# Stable
#channel = "1.70.0" # rustc 1.70.0 (84c898d65 2023-05-13)
# Nightly
channel = "nightly-2023-05-23" # rustc 1.71.0-nightly (8b4b20836 2023-05-22)
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
channel = "nightly-2023-05-22"
Copy link
Collaborator

@koushiro koushiro Aug 1, 2023

Choose a reason for hiding this comment

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

@boundless-forest why change the rust-toolchain config in this PR?

Copy link
Collaborator

@koushiro koushiro Aug 1, 2023

Choose a reason for hiding this comment

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

I use the same rust toolchain config of polkadot releases (https://github.com/paritytech/polkadot/releases/tag/v1.0.0)

channel="nightly-2023-05-22" is not 8b4b20836

rustup show

nightly-2023-05-23-aarch64-apple-darwin
rustc 1.71.0-nightly (8b4b20836 2023-05-22)

Copy link
Collaborator Author

@boundless-forest boundless-forest Aug 2, 2023

Choose a reason for hiding this comment

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

nightly-2023-05-22 is not 8b4b20836

Why? rustc 1.71.0-nightly (8b4b20836 2023-05-22) I understand the 8b4b20836 is the nightly version id of the 2023-05-22.

Is this output misleading?

Copy link
Collaborator

@koushiro koushiro Aug 2, 2023

Choose a reason for hiding this comment

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

you could try to run the command rustup show in the frontier workspace and you didn't change the toolchain of CI at the same time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK. I see your point. How about updating this later in next PR?

components = ["cargo", "clippy", "rustc", "rustfmt", "rust-src"]
profile = "minimal"
targets = ["wasm32-unknown-unknown"]