From af8d110201aaf6e5846f146b73207f7887327638 Mon Sep 17 00:00:00 2001 From: zachs18 <8355914+zachs18@users.noreply.github.com> Date: Thu, 31 Oct 2024 05:30:11 +0000 Subject: [PATCH] More `TransparentWrapper` impls (`core::cmp::Reverse` and `core::num::Saturating`) (#269) * impl TransparentWrapper for core::cmp::Reverse * impl TransparentWrapper for core::num::Saturating, with a new feature for non-base-MSRV stdlib TransparentWrapper impls. * Restrict TransparentWrapper for Reverse to cfg(feature = "transparentwrapper_extra"). * nightly_docs for transparent_wrapper_extra --------- Co-authored-by: Lokathor --- Cargo.toml | 5 +++++ src/transparent.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 67ff212..ee1ccca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,11 @@ aarch64_simd = [] # MSRV 1.59.0: support aarch64 simd types must_cast = [] # MSRV 1.64.0: support the `must` module. +# Adds `TransparentWrapper` impls for stdlib types newer than bytemuck's base MSRV. +# Current MSRV 1.74.0: `core::num::Saturating`. +# MSRV may increase if impls are added for newer types. +transparentwrapper_extra = [] + const_zeroed = [] # MSRV 1.75.0: support const `zeroed()` # Do not use if you can avoid it, because this is **unsound**!!!! diff --git a/src/transparent.rs b/src/transparent.rs index 44f2606..a588eea 100644 --- a/src/transparent.rs +++ b/src/transparent.rs @@ -303,3 +303,20 @@ pub unsafe trait TransparentWrapper { } unsafe impl TransparentWrapper for core::num::Wrapping {} +#[cfg(feature = "transparentwrapper_extra")] +#[cfg_attr( + feature = "nightly_docs", + doc(cfg(feature = "transparentwrapper_extra")) +)] +unsafe impl TransparentWrapper for core::num::Saturating {} + +// Note that `Reverse` existed since Rust 1.19.0, but was only made `#[repr(transparent)]` +// in Rust 1.52.0 (PR: https://github.com/rust-lang/rust/pull/81879), so we have it under +// the same feature as `Saturating`, which was stabilized in Rust 1.74.0, so that this +// impl cannot be used on a version before 1.52.0 where it would be unsound. +#[cfg(feature = "transparentwrapper_extra")] +#[cfg_attr( + feature = "nightly_docs", + doc(cfg(feature = "transparentwrapper_extra")) +)] +unsafe impl TransparentWrapper for core::cmp::Reverse {}