Marginally reduce allocations in lightning-invoice

In aa2f6b47df we refactored
`lightning-invoice` de/serialization to use the new version of
`bech32`, but in order to keep the public API the same we
introduced one allocation we could have skipped.

Instead, here, we replace the public `Utf8Error` with
`FromUtf8Error` which contains the original data which failed
conversion, removing an allocation in the process.
This commit is contained in:
Matt Corallo 2024-10-03 16:54:14 +00:00
parent c5658f6cc2
commit 052e7c3df0
2 changed files with 5 additions and 5 deletions

View file

@ -1,3 +1,4 @@
use alloc::string;
#[cfg(feature = "std")]
use std::error;
#[cfg(not(feature = "std"))]
@ -5,7 +6,6 @@ use core::convert::TryFrom;
use core::fmt;
use core::fmt::{Display, Formatter};
use core::num::ParseIntError;
use core::str;
use core::str::FromStr;
use bech32::primitives::decode::{CheckedHrpstring, CheckedHrpstringError};
@ -613,7 +613,7 @@ impl FromBase32 for Description {
fn from_base32(field_data: &[Fe32]) -> Result<Description, Bolt11ParseError> {
let bytes = Vec::<u8>::from_base32(field_data)?;
let description = String::from(str::from_utf8(&bytes)?);
let description = String::from_utf8(bytes)?;
Ok(Description::new(description).expect(
"Max len is 639=floor(1023*5/8) since the len field is only 10bits long"
))
@ -824,7 +824,7 @@ macro_rules! from_error {
from_error!(Bolt11ParseError::MalformedSignature, bitcoin::secp256k1::Error);
from_error!(Bolt11ParseError::ParseAmountError, ParseIntError);
from_error!(Bolt11ParseError::DescriptionDecodeError, str::Utf8Error);
from_error!(Bolt11ParseError::DescriptionDecodeError, string::FromUtf8Error);
impl From<CheckedHrpstringError> for Bolt11ParseError {
fn from(e: CheckedHrpstringError) -> Self {

View file

@ -51,7 +51,7 @@ use core::num::ParseIntError;
use core::ops::Deref;
use core::slice::Iter;
use core::time::Duration;
use core::str;
use alloc::string;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
@ -98,7 +98,7 @@ pub enum Bolt11ParseError {
MalformedHRP,
TooShortDataPart,
UnexpectedEndOfTaggedFields,
DescriptionDecodeError(str::Utf8Error),
DescriptionDecodeError(string::FromUtf8Error),
PaddingError,
IntegerOverflowError,
InvalidSegWitProgramLength,