Skip to content

Commit 052e7c3

Browse files
committed
Marginally reduce allocations in lightning-invoice
In aa2f6b4 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.
1 parent c5658f6 commit 052e7c3

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

lightning-invoice/src/de.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use alloc::string;
12
#[cfg(feature = "std")]
23
use std::error;
34
#[cfg(not(feature = "std"))]
45
use core::convert::TryFrom;
56
use core::fmt;
67
use core::fmt::{Display, Formatter};
78
use core::num::ParseIntError;
8-
use core::str;
99
use core::str::FromStr;
1010

1111
use bech32::primitives::decode::{CheckedHrpstring, CheckedHrpstringError};
@@ -613,7 +613,7 @@ impl FromBase32 for Description {
613613

614614
fn from_base32(field_data: &[Fe32]) -> Result<Description, Bolt11ParseError> {
615615
let bytes = Vec::<u8>::from_base32(field_data)?;
616-
let description = String::from(str::from_utf8(&bytes)?);
616+
let description = String::from_utf8(bytes)?;
617617
Ok(Description::new(description).expect(
618618
"Max len is 639=floor(1023*5/8) since the len field is only 10bits long"
619619
))
@@ -824,7 +824,7 @@ macro_rules! from_error {
824824

825825
from_error!(Bolt11ParseError::MalformedSignature, bitcoin::secp256k1::Error);
826826
from_error!(Bolt11ParseError::ParseAmountError, ParseIntError);
827-
from_error!(Bolt11ParseError::DescriptionDecodeError, str::Utf8Error);
827+
from_error!(Bolt11ParseError::DescriptionDecodeError, string::FromUtf8Error);
828828

829829
impl From<CheckedHrpstringError> for Bolt11ParseError {
830830
fn from(e: CheckedHrpstringError) -> Self {

lightning-invoice/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use core::num::ParseIntError;
5151
use core::ops::Deref;
5252
use core::slice::Iter;
5353
use core::time::Duration;
54-
use core::str;
54+
use alloc::string;
5555

5656
#[cfg(feature = "serde")]
5757
use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
@@ -98,7 +98,7 @@ pub enum Bolt11ParseError {
9898
MalformedHRP,
9999
TooShortDataPart,
100100
UnexpectedEndOfTaggedFields,
101-
DescriptionDecodeError(str::Utf8Error),
101+
DescriptionDecodeError(string::FromUtf8Error),
102102
PaddingError,
103103
IntegerOverflowError,
104104
InvalidSegWitProgramLength,

0 commit comments

Comments
 (0)