Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use magnus::{
use tokio::sync::mpsc::error::SendError;

const ERROR_PREDICATES_IVAR: &str = "wreq_error_predicates";
type ErrorPredicateBits = u64;

const RACE_CONDITION_ERROR_MSG: &str = r#"Due to Rust's memory management with borrowing,
you cannot use certain instances multiple times as they may be consumed.
Expand Down Expand Up @@ -57,8 +58,8 @@ macro_rules! define_error_mapping {
const CLASSIFICATION_ORDER: &'static [Self] = &[$(Self::$predicate),+];

/// Return this predicate's position in the compact Ruby metadata.
const fn mask(self) -> u16 {
1_u16 << (self as u8)
const fn mask(self) -> ErrorPredicateBits {
1 << (self as u8)
}

/// Evaluate this predicate before the native error is consumed.
Expand All @@ -77,7 +78,7 @@ macro_rules! define_error_mapping {
}

const _: () =
assert!(ErrorPredicate::CLASSIFICATION_ORDER.len() <= u16::BITS as usize);
assert!(ErrorPredicate::CLASSIFICATION_ORDER.len() <= ErrorPredicateBits::BITS as usize);

$(
$(define_exception!($class, $ruby_name, exception_runtime_error);)?
Expand Down Expand Up @@ -131,16 +132,16 @@ define_error_mapping! {

/// Native predicates retained after consuming a wreq error.
#[derive(Clone, Copy, Default)]
struct ErrorPredicates(u16);
struct ErrorPredicates(ErrorPredicateBits);

impl ErrorPredicates {
/// Restore predicates from compact Ruby metadata.
const fn from_bits(bits: u16) -> Self {
const fn from_bits(bits: ErrorPredicateBits) -> Self {
Self(bits)
}

/// Return the compact representation stored on the Ruby exception.
const fn bits(self) -> u16 {
const fn bits(self) -> ErrorPredicateBits {
self.0
}

Expand Down Expand Up @@ -339,7 +340,7 @@ fn wreq_error_class(ruby: &Ruby, predicates: ErrorPredicates) -> ExceptionClass
/// Read one native predicate from a Ruby error, defaulting to false.
fn error_has_predicate(rb_self: RObject, predicate: ErrorPredicate) -> Result<bool, MagnusError> {
rb_self
.ivar_get::<_, Option<u16>>(ERROR_PREDICATES_IVAR)
.ivar_get::<_, Option<ErrorPredicateBits>>(ERROR_PREDICATES_IVAR)
.map(|bits| bits.is_some_and(|bits| ErrorPredicates::from_bits(bits).contains(predicate)))
}

Expand Down