Skip to content

Commit

Permalink
Merge #642: Remove generics from Error by making fragment a String
Browse files Browse the repository at this point in the history
0afd832 Remove generics from Error by making fragment a String (Riccardo Casatta)

Pull request description:

  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.

ACKs for top commit:
  apoelstra:
    ACK 0afd832

Tree-SHA512: f6980ee43f93278f0c7dcf5000e67d1e201549a77996bc66c144ea978d3820debbb82a082ad9e2b9a57d91e62a879268e8c914b4e922bd1619e0f0763916b1ea
  • Loading branch information
apoelstra committed Mar 4, 2024
2 parents fe8f040 + 0afd832 commit 02504cb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 62 deletions.
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,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 02504cb

Please sign in to comment.