From 5f7eff02e1e01bfd87d1634b4f330ea37bf7184a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Tue, 21 Apr 2020 14:50:14 +0200 Subject: [PATCH] Expose "the" RawBitFlags trait in the public API Resolves #23. This is necessary to make blanket implementations possible. We export a proxy trait, and keep the real thing in the _internal module. This "prevents" users from relying on the trait items (either by implementing the trait manually or calling its methods), while still being able to name the trait in generic bounds. --- enumflags/src/fallible.rs | 2 +- enumflags/src/formatting.rs | 2 +- enumflags/src/lib.rs | 8 +++++--- enumflags_derive/src/lib.rs | 2 ++ test_suite/tests/requires_std.rs | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/enumflags/src/fallible.rs b/enumflags/src/fallible.rs index 2cf92df..7b70790 100644 --- a/enumflags/src/fallible.rs +++ b/enumflags/src/fallible.rs @@ -1,7 +1,7 @@ use core::convert::TryFrom; use core::fmt; use super::BitFlags; -use super::_internal::RawBitFlags; +use super::RawBitFlags; // Coherence doesn't let us use a generic type here. Work around by implementing // for each integer type manually. diff --git a/enumflags/src/formatting.rs b/enumflags/src/formatting.rs index ca2c951..a0cfcf9 100644 --- a/enumflags/src/formatting.rs +++ b/enumflags/src/formatting.rs @@ -1,5 +1,5 @@ use core::fmt::{self, Debug, Binary}; -use crate::{BitFlags, _internal::RawBitFlags}; +use crate::{BitFlags, RawBitFlags}; impl fmt::Debug for BitFlags where diff --git a/enumflags/src/lib.rs b/enumflags/src/lib.rs index 3ebb337..845bb05 100644 --- a/enumflags/src/lib.rs +++ b/enumflags/src/lib.rs @@ -99,6 +99,10 @@ extern crate enumflags2_derive; #[doc(hidden)] pub use enumflags2_derive::BitFlags_internal as BitFlags; +/// A trait automatically implemented by `derive(BitFlags)` to make the enum +/// a valid type parameter for `BitFlags`. +pub trait RawBitFlags: Copy + Clone + 'static + _internal::RawBitFlags {} + /// While the module is public, this is only the case because it needs to be /// accessed by the derive macro. Do not use this directly. Stability guarantees /// don't apply. @@ -163,8 +167,6 @@ mod formatting; mod fallible; pub use fallible::FromBitsError; -use _internal::RawBitFlags; - /// Represents a set of flags of some type `T`. /// The type must have the `#[derive(BitFlags)]` attribute applied. #[derive(Copy, Clone, Eq, Hash)] @@ -391,7 +393,7 @@ mod impl_serde { extern crate serde; use self::serde::{Serialize, Deserialize}; use self::serde::de::{Error, Unexpected}; - use super::{BitFlags, _internal::RawBitFlags}; + use super::{BitFlags, RawBitFlags}; impl<'a, T> Deserialize<'a> for BitFlags where diff --git a/enumflags_derive/src/lib.rs b/enumflags_derive/src/lib.rs index bfbcc7f..0d6303c 100644 --- a/enumflags_derive/src/lib.rs +++ b/enumflags_derive/src/lib.rs @@ -241,5 +241,7 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum) concat!("BitFlags<", stringify!(#ident), ">") } } + + impl ::enumflags2::RawBitFlags for #ident {} }) } diff --git a/test_suite/tests/requires_std.rs b/test_suite/tests/requires_std.rs index 3f02db4..a6c1d51 100644 --- a/test_suite/tests/requires_std.rs +++ b/test_suite/tests/requires_std.rs @@ -85,7 +85,7 @@ fn format() { #[test] fn debug_generic() { - use enumflags2::{BitFlags, _internal::RawBitFlags}; + use enumflags2::{BitFlags, RawBitFlags}; #[derive(Debug)] struct Debug(BitFlags);