impl FromStr for Memo

Memo::from_str was previously shadowing a built-in trait method.
This commit is contained in:
Jack Grigg
2019-08-19 00:21:10 +01:00
parent 7c1d4d9a5b
commit 0c7eb84d36

View File

@@ -106,11 +106,6 @@ impl Memo {
} }
} }
/// Returns a `Memo` containing the given string, or `None` if the string is too long.
pub fn from_str(memo: &str) -> Option<Memo> {
Memo::from_bytes(memo.as_bytes())
}
/// Returns the underlying bytes of the `Memo`. /// Returns the underlying bytes of the `Memo`.
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
&self.0[..] &self.0[..]
@@ -134,6 +129,15 @@ impl Memo {
} }
} }
impl str::FromStr for Memo {
type Err = ();
/// Returns a `Memo` containing the given string, or an error if the string is too long.
fn from_str(memo: &str) -> Result<Self, Self::Err> {
Memo::from_bytes(memo.as_bytes()).ok_or(())
}
}
pub fn generate_esk<R: RngCore + CryptoRng>(rng: &mut R) -> Fs { pub fn generate_esk<R: RngCore + CryptoRng>(rng: &mut R) -> Fs {
// create random 64 byte buffer // create random 64 byte buffer
let mut buffer = [0u8; 64]; let mut buffer = [0u8; 64];
@@ -557,6 +561,7 @@ mod tests {
use pairing::bls12_381::{Bls12, Fr, FrRepr}; use pairing::bls12_381::{Bls12, Fr, FrRepr};
use rand_core::{CryptoRng, RngCore}; use rand_core::{CryptoRng, RngCore};
use rand_os::OsRng; use rand_os::OsRng;
use std::str::FromStr;
use super::{ use super::{
kdf_sapling, prf_ock, sapling_ka_agree, try_sapling_compact_note_decryption, kdf_sapling, prf_ock, sapling_ka_agree, try_sapling_compact_note_decryption,
@@ -661,7 +666,8 @@ mod tests {
0x74, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68 0x74, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68
]) ])
); );
assert!(Memo::from_str( assert_eq!(
Memo::from_str(
"thiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiis \ "thiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiis \
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiis \ iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiis \
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \
@@ -669,8 +675,9 @@ mod tests {
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong \ looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong \
meeeeeeeeeeeeeeeeeeemooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \ meeeeeeeeeeeeeeeeeeemooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \
but it's now a bit too long" but it's now a bit too long"
) ),
.is_none()); Err(())
);
} }
#[test] #[test]