Skip to content

Commit 22aaafe

Browse files
Add AsRef and AsMut for AccountId20 (polkadot-evm#1139)
* Update toolchain toml config * Add helper functions
1 parent 0bb1c7f commit 22aaafe

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

primitives/account/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ impl From<[u8; 20]> for AccountId20 {
8484
}
8585
}
8686

87+
impl<'a> TryFrom<&'a [u8]> for AccountId20 {
88+
type Error = ();
89+
fn try_from(x: &'a [u8]) -> Result<AccountId20, ()> {
90+
if x.len() == 20 {
91+
let mut data = [0; 20];
92+
data.copy_from_slice(x);
93+
Ok(AccountId20(data))
94+
} else {
95+
Err(())
96+
}
97+
}
98+
}
99+
87100
impl From<AccountId20> for [u8; 20] {
88101
fn from(val: AccountId20) -> Self {
89102
val.0
@@ -102,6 +115,30 @@ impl From<AccountId20> for H160 {
102115
}
103116
}
104117

118+
impl AsRef<[u8]> for AccountId20 {
119+
fn as_ref(&self) -> &[u8] {
120+
&self.0[..]
121+
}
122+
}
123+
124+
impl AsMut<[u8]> for AccountId20 {
125+
fn as_mut(&mut self) -> &mut [u8] {
126+
&mut self.0[..]
127+
}
128+
}
129+
130+
impl AsRef<[u8; 20]> for AccountId20 {
131+
fn as_ref(&self) -> &[u8; 20] {
132+
&self.0
133+
}
134+
}
135+
136+
impl AsMut<[u8; 20]> for AccountId20 {
137+
fn as_mut(&mut self) -> &mut [u8; 20] {
138+
&mut self.0
139+
}
140+
}
141+
105142
impl From<ecdsa::Public> for AccountId20 {
106143
fn from(pk: ecdsa::Public) -> Self {
107144
let decompressed = libsecp256k1::PublicKey::parse_compressed(&pk.0)

rust-toolchain.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
[toolchain]
2-
# Stable
3-
#channel = "1.70.0" # rustc 1.70.0 (84c898d65 2023-05-13)
4-
# Nightly
5-
channel = "nightly-2023-05-23" # rustc 1.71.0-nightly (8b4b20836 2023-05-22)
6-
components = ["rustfmt", "clippy"]
7-
targets = ["wasm32-unknown-unknown"]
8-
profile = "minimal"
2+
channel = "nightly-2023-05-22"
3+
components = ["cargo", "clippy", "rustc", "rustfmt", "rust-src"]
4+
profile = "minimal"
5+
targets = ["wasm32-unknown-unknown"]

0 commit comments

Comments
 (0)