Skip to content

Commit

Permalink
polyfill: Extend cold_exhaustive_error to struct types.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jan 12, 2025
1 parent bc61de9 commit 7b95153
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
14 changes: 3 additions & 11 deletions src/aead/overlapping/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#![cfg_attr(not(test), allow(dead_code))]

pub use self::len_mismatch_error::LenMismatchError;
use super::Overlapping;
use core::array::TryFromSliceError;

Expand Down Expand Up @@ -58,15 +59,6 @@ impl<T, const N: usize> Array<'_, T, N> {
}
}

pub struct LenMismatchError {
#[allow(dead_code)]
len: usize,
}

impl LenMismatchError {
#[cold]
#[inline(never)]
fn new(len: usize) -> Self {
Self { len }
}
cold_exhaustive_error! {
struct len_mismatch_error::LenMismatchError { len: usize }
}
11 changes: 3 additions & 8 deletions src/aead/overlapping/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

pub use self::index_error::IndexError;
use super::{Array, LenMismatchError};
use core::{mem, ops::RangeFrom};

Expand Down Expand Up @@ -130,12 +131,6 @@ impl<T> Overlapping<'_, T> {
}
}

pub struct IndexError(#[allow(dead_code)] usize);

impl IndexError {
#[cold]
#[inline(never)]
fn new(index: usize) -> Self {
Self(index)
}
cold_exhaustive_error! {
struct index_error::IndexError { index: usize }
}
23 changes: 23 additions & 0 deletions src/polyfill/cold_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@
/// work around the lack of the ability to mark an enum variant `#[cold]` and
/// `#[inline(never)]`.
macro_rules! cold_exhaustive_error {
// struct
{
struct $mod_name:ident::$Error:ident {
$field:ident: $ValueType:ty
}
} => {
mod $mod_name {
#[allow(unused_imports)]
use super::*; // So `$ValueType` is in scope.

pub struct $Error { #[allow(dead_code)] $field: $ValueType }

impl $Error {
#[cold]
#[inline(never)]
pub(super) fn new($field: $ValueType) -> Self {
Self { $field }
}
}
}
};

// enum
{
enum $mod_name:ident::$Error:ident {
$(
Expand Down

0 comments on commit 7b95153

Please sign in to comment.