Skip to content

Commit

Permalink
Add AsRef and AsMut for AccountId20 (#1139)
Browse files Browse the repository at this point in the history
* Update toolchain toml config

* Add helper functions
  • Loading branch information
boundless-forest authored Aug 1, 2023
1 parent 0bb1c7f commit 22aaafe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
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"
components = ["cargo", "clippy", "rustc", "rustfmt", "rust-src"]
profile = "minimal"
targets = ["wasm32-unknown-unknown"]

0 comments on commit 22aaafe

Please sign in to comment.