Skip to content

Commit

Permalink
Pass Hrp by reference
Browse files Browse the repository at this point in the history
The `Hrp` type is a struct containing an 83 byte arary, as such it
should be passed by reference (borrowed) not copied.
  • Loading branch information
tcharding committed Oct 19, 2023
1 parent fb3ff2d commit c65666e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion embedded/no-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> ! {
let data = [0x00u8, 0x01, 0x02];
let hrp = Hrp::parse("bech32").expect("failed to parse hrp");

bech32::encode_to_fmt::<Bech32, _>(&mut encoded, hrp, &data)
bech32::encode_to_fmt::<Bech32, _>(&mut encoded, &hrp, &data)
.expect("failed to encode");
test(&*encoded == "bech321qqqsyrhqy2a");

Expand Down
2 changes: 1 addition & 1 deletion embedded/with-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() -> ! {
let data = [0x00u8, 0x01, 0x02];
let hrp = Hrp::parse("bech32").expect("failed to parse hrp");

let encoded = bech32::encode::<Bech32m>(hrp, &data).expect("failed to encode");
let encoded = bech32::encode::<Bech32m>(&hrp, &data).expect("failed to encode");
test(encoded == "bech321qqqsyktsg0l".to_string());

hprintln!("{}", encoded).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/encode_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ fn do_test(data: &[u8]) {
match Hrp::parse(&s) {
Err(_) => return,
Ok(hrp) => {
if let Ok(address) = bech32::encode::<Bech32m>(hrp, dp) {
if let Ok(address) = bech32::encode::<Bech32m>(&hrp, dp) {
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
assert_eq!(bech32::encode::<Bech32m>(&hrp, &data).unwrap(), address);
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//!
//! // Encode arbitrary data using "abc" as the human-readable part and append a bech32m checksum.
//! let hrp = Hrp::parse("abc").expect("valid hrp");
//! let address = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode address");
//! let address = bech32::encode::<Bech32m>(&hrp, &DATA).expect("failed to encode address");
//! assert_eq!(address, ADDR);
//!
//! // Encode arbitrary data as a Bitcoin taproot address.
Expand All @@ -46,7 +46,7 @@
//!
//! // No-alloc: Encode without allocating (ignoring that String::new() allocates :).
//! let mut buf = String::new();
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("failed to encode to buffer");
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, &hrp, &DATA).expect("failed to encode to buffer");
//! assert_eq!(buf, ADDR);
//! # }
//! ```
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn decode(s: &str) -> Result<(Hrp, Vec<u8>), DecodeError> {
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
#[cfg(feature = "alloc")]
#[inline]
pub fn encode<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error> {
pub fn encode<Ck: Checksum>(hrp: &Hrp, data: &[u8]) -> Result<String, fmt::Error> {
encode_lower::<Ck>(hrp, data)
}

Expand All @@ -224,7 +224,7 @@ pub fn encode<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error>
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
#[cfg(feature = "alloc")]
#[inline]
pub fn encode_lower<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error> {
pub fn encode_lower<Ck: Checksum>(hrp: &Hrp, data: &[u8]) -> Result<String, fmt::Error> {
let mut buf = String::new();
encode_lower_to_fmt::<Ck, String>(&mut buf, hrp, data)?;
Ok(buf)
Expand All @@ -236,7 +236,7 @@ pub fn encode_lower<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
#[cfg(feature = "alloc")]
#[inline]
pub fn encode_upper<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error> {
pub fn encode_upper<Ck: Checksum>(hrp: &Hrp, data: &[u8]) -> Result<String, fmt::Error> {
let mut buf = String::new();
encode_upper_to_fmt::<Ck, String>(&mut buf, hrp, data)?;
Ok(buf)
Expand All @@ -249,7 +249,7 @@ pub fn encode_upper<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::
#[inline]
pub fn encode_to_fmt<Ck: Checksum, W: fmt::Write>(
fmt: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), fmt::Error> {
encode_lower_to_fmt::<Ck, W>(fmt, hrp, data)
Expand All @@ -262,11 +262,11 @@ pub fn encode_to_fmt<Ck: Checksum, W: fmt::Write>(
#[inline]
pub fn encode_lower_to_fmt<Ck: Checksum, W: fmt::Write>(
fmt: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), fmt::Error> {
let iter = data.iter().copied().bytes_to_fes();
let chars = iter.with_checksum::<Ck>(&hrp).chars();
let chars = iter.with_checksum::<Ck>(hrp).chars();
for c in chars {
fmt.write_char(c)?;
}
Expand All @@ -280,11 +280,11 @@ pub fn encode_lower_to_fmt<Ck: Checksum, W: fmt::Write>(
#[inline]
pub fn encode_upper_to_fmt<Ck: Checksum, W: fmt::Write>(
fmt: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), fmt::Error> {
let iter = data.iter().copied().bytes_to_fes();
let chars = iter.with_checksum::<Ck>(&hrp).chars();
let chars = iter.with_checksum::<Ck>(hrp).chars();
for c in chars {
fmt.write_char(c.to_ascii_uppercase())?;
}
Expand All @@ -299,7 +299,7 @@ pub fn encode_upper_to_fmt<Ck: Checksum, W: fmt::Write>(
#[inline]
pub fn encode_to_writer<Ck: Checksum, W: std::io::Write>(
w: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), std::io::Error> {
encode_lower_to_writer::<Ck, W>(w, hrp, data)
Expand All @@ -313,11 +313,11 @@ pub fn encode_to_writer<Ck: Checksum, W: std::io::Write>(
#[inline]
pub fn encode_lower_to_writer<Ck: Checksum, W: std::io::Write>(
w: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), std::io::Error> {
let iter = data.iter().copied().bytes_to_fes();
let chars = iter.with_checksum::<Ck>(&hrp).chars();
let chars = iter.with_checksum::<Ck>(hrp).chars();
for c in chars {
w.write_all(&[c as u8])?;
}
Expand All @@ -332,11 +332,11 @@ pub fn encode_lower_to_writer<Ck: Checksum, W: std::io::Write>(
#[inline]
pub fn encode_upper_to_writer<Ck: Checksum, W: std::io::Write>(
w: &mut W,
hrp: Hrp,
hrp: &Hrp,
data: &[u8],
) -> Result<(), std::io::Error> {
let iter = data.iter().copied().bytes_to_fes();
let chars = iter.with_checksum::<Ck>(&hrp).chars();
let chars = iter.with_checksum::<Ck>(hrp).chars();
for c in chars {
w.write_all(&[c.to_ascii_uppercase() as u8])?;
}
Expand Down Expand Up @@ -402,15 +402,15 @@ mod tests {
#[test]
fn encode_bech32m() {
let hrp = Hrp::parse_unchecked("test");
let got = encode::<Bech32m>(hrp, &DATA).expect("failed to encode");
let got = encode::<Bech32m>(&hrp, &DATA).expect("failed to encode");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kmz4lky";
assert_eq!(got, want);
}

#[test]
fn encode_bech32_lower() {
let hrp = Hrp::parse_unchecked("test");
let got = encode_lower::<Bech32>(hrp, &DATA).expect("failed to encode");
let got = encode_lower::<Bech32>(&hrp, &DATA).expect("failed to encode");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kw79nnx";
assert_eq!(got, want);
}
Expand All @@ -420,7 +420,7 @@ mod tests {
fn encode_bech32_lower_to_writer() {
let hrp = Hrp::parse_unchecked("test");
let mut buf = Vec::new();
encode_lower_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("failed to encode");
encode_lower_to_writer::<Bech32, _>(&mut buf, &hrp, &DATA).expect("failed to encode");

let got = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kw79nnx";
Expand All @@ -430,7 +430,7 @@ mod tests {
#[test]
fn encode_bech32_upper() {
let hrp = Hrp::parse_unchecked("test");
let got = encode_upper::<Bech32>(hrp, &DATA).expect("failed to encode");
let got = encode_upper::<Bech32>(&hrp, &DATA).expect("failed to encode");
let want = "TEST1LU08D6QEJXTDG4Y5R3ZARVARY0C5XW7KW79NNX";
assert_eq!(got, want);
}
Expand All @@ -440,7 +440,7 @@ mod tests {
fn encode_bech32_upper_to_writer() {
let hrp = Hrp::parse_unchecked("test");
let mut buf = Vec::new();
encode_upper_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("failed to encode");
encode_upper_to_writer::<Bech32, _>(&mut buf, &hrp, &DATA).expect("failed to encode");

let got = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "TEST1LU08D6QEJXTDG4Y5R3ZARVARY0C5XW7KW79NNX";
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ mod tests {
"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio";

let hrp = Hrp::parse_unchecked(hrps);
let s = crate::encode::<Bech32>(hrp, &[]).expect("failed to encode empty buffer");
let s = crate::encode::<Bech32>(&hrp, &[]).expect("failed to encode empty buffer");

let unchecked = UncheckedHrpstring::new(&s).expect("failed to parse address");
assert_eq!(unchecked.hrp(), hrp);
Expand Down

0 comments on commit c65666e

Please sign in to comment.