Skip to content

Commit

Permalink
chore: box value (#2152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Mar 6, 2025
1 parent 9792cb1 commit fd789f1
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions crates/consensus/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Helper errors.
use alloc::string::{String, ToString};
use alloc::{
boxed::Box,
string::{String, ToString},
};

/// Helper type that is [`core::error::Error`] and wraps a value and an error message.
///
Expand All @@ -9,13 +12,13 @@ use alloc::string::{String, ToString};
#[error("{msg}")]
pub struct ValueError<T> {
msg: String,
value: T,
value: Box<T>,
}

impl<T> ValueError<T> {
/// Creates a new error with the given value and error message.
pub fn new(value: T, msg: impl core::fmt::Display) -> Self {
Self { msg: msg.to_string(), value }
Self { msg: msg.to_string(), value: Box::new(value) }
}

/// Converts the value to the given alternative that is `From<T>`.
Expand All @@ -28,12 +31,12 @@ impl<T> ValueError<T> {

/// Maps the error's value with the given closure.
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ValueError<U> {
ValueError { msg: self.msg, value: f(self.value) }
ValueError { msg: self.msg, value: Box::new(f(*self.value)) }
}

/// Consumes the type and returns the underlying value.
pub fn into_value(self) -> T {
self.value
*self.value
}

/// Returns a reference to the value.
Expand Down

0 comments on commit fd789f1

Please sign in to comment.