Skip to content

Commit

Permalink
Remove generics from Error by turing fragment into String
Browse files Browse the repository at this point in the history
This cause a reduction of IR lines emitted by the compiler of about 2%
(using cargo llvm-lines), so this should improve compile times.

I've seen using the parse example to check the produced binary size, but it's
a small snippet I am not sure it calls a representative portion of the API
surface.

From a library perspective this is equivalent since the field is used only
to be converted to string.

In theory externally someone could use the field in the error to do things so
it comes at a cost.
  • Loading branch information
RCasatta committed Feb 26, 2024
1 parent 18b1b30 commit 8ec4156
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 61 deletions.
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,8 @@ impl error::Error for Error {
}

#[doc(hidden)]
impl<Pk, Ctx> From<miniscript::types::Error<Pk, Ctx>> for Error
where
Pk: MiniscriptKey,
Ctx: ScriptContext,
{
fn from(e: miniscript::types::Error<Pk, Ctx>) -> Error { Error::TypeCheck(e.to_string()) }
impl From<miniscript::types::Error> for Error {
fn from(e: miniscript::types::Error) -> Error { Error::TypeCheck(e.to_string()) }
}

#[doc(hidden)]
Expand Down
20 changes: 10 additions & 10 deletions src/miniscript/types/extra_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ impl Property for ExtData {
fn type_check_with_child<Pk, Ctx, C>(
_fragment: &Terminal<Pk, Ctx>,
mut _child: C,
) -> Result<Self, Error<Pk, Ctx>>
) -> Result<Self, Error>
where
C: FnMut(usize) -> Self,
Pk: MiniscriptKey,
Expand All @@ -871,13 +871,13 @@ impl Property for ExtData {

/// Compute the type of a fragment assuming all the children of
/// Miniscript have been computed already.
fn type_check<Pk, Ctx>(fragment: &Terminal<Pk, Ctx>) -> Result<Self, Error<Pk, Ctx>>
fn type_check<Pk, Ctx>(fragment: &Terminal<Pk, Ctx>) -> Result<Self, Error>
where
Ctx: ScriptContext,
Pk: MiniscriptKey,
{
let wrap_err = |result: Result<Self, ErrorKind>| {
result.map_err(|kind| Error { fragment: fragment.clone(), error: kind })
result.map_err(|kind| Error { fragment_string: fragment.to_string(), error: kind })
};

let ret = match *fragment {
Expand All @@ -888,13 +888,13 @@ impl Property for ExtData {
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
if k == 0 {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::ZeroThreshold,
});
}
if k > pks.len() {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::OverThreshold(k, pks.len()),
});
}
Expand All @@ -910,7 +910,7 @@ impl Property for ExtData {
// only consumes 4 bytes from the stack.
if t == absolute::LockTime::ZERO.into() {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::InvalidTime,
});
}
Expand All @@ -919,7 +919,7 @@ impl Property for ExtData {
Terminal::Older(t) => {
if t == Sequence::ZERO || !t.is_relative_lock_time() {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::InvalidTime,
});
}
Expand Down Expand Up @@ -975,20 +975,20 @@ impl Property for ExtData {
Terminal::Thresh(k, ref subs) => {
if k == 0 {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::ZeroThreshold,
});
}
if k > subs.len() {
return Err(Error {
fragment: fragment.clone(),
fragment_string: fragment.to_string(),
error: ErrorKind::OverThreshold(k, subs.len()),
});
}

let res = Self::threshold(k, subs.len(), |n| Ok(subs[n].ext));

res.map_err(|kind| Error { fragment: fragment.clone(), error: kind })
res.map_err(|kind| Error { fragment_string: fragment.to_string(), error: kind })
}
};
if let Ok(ref ret) = ret {
Expand Down
Loading

0 comments on commit 8ec4156

Please sign in to comment.