Skip to content

Commit

Permalink
Remove unreleased arrayvec code
Browse files Browse the repository at this point in the history
We have a bunch of feature gated code that was added to enable
allocationless encoding/decoding, this is now supported by the
`primitives` module so we can remove all the "arrayvec" code.

Implement the `no-allocator` embedded test using the new `primitives`
API and then remove all the "arrayvec" stuff.
  • Loading branch information
tcharding committed Aug 1, 2023
1 parent 581a913 commit e301967
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 106 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ default = ["std"]
std = ["alloc"]
alloc = []

[dependencies.arrayvec]
version = "0.7.1"
default-features = false
optional = true

[target.'cfg(mutate)'.dev-dependencies]
mutagen = { git = "https://github.com/llogiq/mutagen" }
6 changes: 0 additions & 6 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ if [ "${DO_FEATURE_MATRIX-false}" = true ]; then
build_and_test "std"
build_and_test "alloc"
build_and_test "std alloc"
# arrayvec breaks the MSRV
if [ $MSRV = false ]; then
build_and_test "arrayvec"
build_and_test "std arrayvec"
build_and_test "alloc arrayvec"
fi
fi

# Build the docs if told to (this only works with the nightly toolchain)
Expand Down
2 changes: 1 addition & 1 deletion embedded/no-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
arrayvec = { version = "0.7.1", default-features = false }
bech32 = { path = "../../", default-features = false, features = ["arrayvec"] }
bech32 = { path = "../../", default-features = false }

[[bin]]
name = "no-allocator"
Expand Down
18 changes: 6 additions & 12 deletions embedded/no-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#![no_std]

use arrayvec::{ArrayString, ArrayVec};
use bech32::{self, u5, ComboError, FromBase32, Hrp, ToBase32, Variant};
use bech32::{self, u5, Hrp, Variant, ByteIterExt, Bech32};
use bech32::primitives::decode::CheckedHrpstring;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use panic_halt as _;
Expand All @@ -19,9 +20,7 @@ use panic_halt as _;
fn main() -> ! {
let mut encoded = ArrayString::<30>::new();

let mut base32 = ArrayVec::<u5, 30>::new();

[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();
let base32 = [0x00u8, 0x01, 0x02].iter().copied().bytes_to_fes().collect::<ArrayVec<u5, 30>>();

let hrp = Hrp::parse("bech32").unwrap();

Expand All @@ -30,16 +29,11 @@ fn main() -> ! {

hprintln!("{}", encoded).unwrap();

let mut decoded = ArrayVec::<u5, 30>::new();

let mut scratch = ArrayVec::<u5, 30>::new();
let unchecked = CheckedHrpstring::new::<Bech32>(&encoded).unwrap();

let (got_hrp, data, variant) =
bech32::decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
test(got_hrp == hrp);
let res = ArrayVec::<u8, 30>::from_base32(&data).unwrap();
test(unchecked.hrp() == hrp);
let res = unchecked.byte_iter().collect::<ArrayVec<u8, 30>>();
test(&res == [0x00, 0x01, 0x02].as_ref());
test(variant == Variant::Bech32);

debug::exit(debug::EXIT_SUCCESS);

Expand Down
82 changes: 0 additions & 82 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ pub use crate::primitives::{Bech32, Bech32m};
mod error;
pub mod primitives;

#[cfg(feature = "arrayvec")]
use arrayvec::{ArrayVec, CapacityError};
pub use primitives::gf32::Fe32 as u5;

/// Interface to write `u5`s into a sink.
Expand Down Expand Up @@ -164,21 +162,6 @@ pub trait FromBase32: Sized {

macro_rules! write_base_n {
{ $tr:ident, $ty:ident, $meth:ident } => {
#[cfg(feature = "arrayvec")]
impl<const L: usize> $tr for ArrayVec<$ty, L> {
type Error = CapacityError;

fn write(&mut self, data: &[$ty]) -> Result<(), Self::Error> {
self.try_extend_from_slice(data)?;
Ok(())
}

fn $meth(&mut self, data: $ty) -> Result<(), Self::Error> {
self.push(data);
Ok(())
}
}

#[cfg(feature = "alloc")]
impl $tr for Vec<$ty> {
type Error = Infallible;
Expand All @@ -199,41 +182,6 @@ macro_rules! write_base_n {
write_base_n! { WriteBase32, u5, write_u5 }
write_base_n! { WriteBase256, u8, write_u8 }

#[cfg(feature = "arrayvec")]
#[derive(Clone, Debug, PartialEq, Eq)]
/// Combination of Errors for use with array vec
pub enum ComboError {
/// Error from this crate
Bech32Error(Error),
/// Error from `arrayvec`.
WriteError(CapacityError),
}
#[cfg(feature = "arrayvec")]
impl From<Error> for ComboError {
fn from(e: Error) -> ComboError { ComboError::Bech32Error(e) }
}
#[cfg(feature = "arrayvec")]
impl From<CapacityError> for ComboError {
fn from(e: CapacityError) -> ComboError { ComboError::WriteError(e) }
}
#[cfg(feature = "arrayvec")]
impl From<hrp::Error> for ComboError {
fn from(e: hrp::Error) -> ComboError { ComboError::Bech32Error(Error::Hrp(e)) }
}

#[cfg(feature = "arrayvec")]
impl<const L: usize> FromBase32 for ArrayVec<u8, L> {
type Error = ComboError;

/// Convert base32 to base256, removes null-padding if present, returns
/// `Err(Error::InvalidPadding)` if padding bits are unequal `0`
fn from_base32(b32: &[u5]) -> Result<Self, Self::Error> {
let mut ret: ArrayVec<u8, L> = ArrayVec::new();
convert_bits_in::<ComboError, _, _>(b32, 5, 8, false, &mut ret)?;
Ok(ret)
}
}

#[cfg(feature = "alloc")]
impl FromBase32 for Vec<u8> {
type Error = Error;
Expand Down Expand Up @@ -861,9 +809,6 @@ where

#[cfg(test)]
mod tests {
#[cfg(feature = "arrayvec")]
use arrayvec::ArrayString;

use super::*;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -1164,33 +1109,6 @@ mod tests {
assert!(u5::try_from(32_u128).is_err());
}

#[test]
#[cfg(feature = "arrayvec")]
fn test_arrayvec() {
let mut encoded = ArrayString::<30>::new();

let mut base32 = ArrayVec::<u5, 30>::new();

[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();

let bech32_hrp = Hrp::parse("bech32").expect("bech32 is valid");
encode_to_fmt_anycase(&mut encoded, bech32_hrp, &base32, Variant::Bech32).unwrap().unwrap();
assert_eq!(&*encoded, "bech321qqqsyrhqy2a");

println!("{}", encoded);

let mut decoded = ArrayVec::<u5, 30>::new();

let mut scratch = ArrayVec::<u5, 30>::new();

let (hrp, data, variant) =
decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
assert_eq!(hrp.to_string(), "bech32");
let res = ArrayVec::<u8, 30>::from_base32(data).unwrap();
assert_eq!(&res, [0x00, 0x01, 0x02].as_ref());
assert_eq!(variant, Variant::Bech32);
}

#[test]
#[cfg(feature = "alloc")]
fn decode_bitcoin_bech32_address() {
Expand Down

0 comments on commit e301967

Please sign in to comment.