Skip to content

Commit f3b9602

Browse files
committed
feat: Add useful convertion trait implementation
1 parent 4eabc1a commit f3b9602

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/min_curve/encoding.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1+
use crate::EncodingError;
2+
use core::convert::TryFrom;
3+
14
#[derive(Copy, Clone, Default, Eq, Ord, PartialOrd, PartialEq, Debug)]
25
pub struct Encoding(pub [u8; 32]);
6+
7+
impl TryFrom<&[u8]> for Encoding {
8+
type Error = EncodingError;
9+
10+
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
11+
if bytes.len() == 32 {
12+
let mut arr = [0u8; 32];
13+
arr.copy_from_slice(&bytes[0..32]);
14+
Ok(Encoding(arr))
15+
} else {
16+
Err(EncodingError::InvalidSliceLength)
17+
}
18+
}
19+
}
20+
21+
impl From<[u8; 32]> for Encoding {
22+
fn from(bytes: [u8; 32]) -> Encoding {
23+
Encoding(bytes)
24+
}
25+
}
26+
27+
impl From<Encoding> for [u8; 32] {
28+
fn from(enc: Encoding) -> [u8; 32] {
29+
enc.0
30+
}
31+
}

0 commit comments

Comments
 (0)