Skip to content

Commit 5f7eff0

Browse files
committed
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.
1 parent e2d2f68 commit 5f7eff0

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

enumflags/src/fallible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::TryFrom;
22
use core::fmt;
33
use super::BitFlags;
4-
use super::_internal::RawBitFlags;
4+
use super::RawBitFlags;
55

66
// Coherence doesn't let us use a generic type here. Work around by implementing
77
// for each integer type manually.

enumflags/src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt::{self, Debug, Binary};
2-
use crate::{BitFlags, _internal::RawBitFlags};
2+
use crate::{BitFlags, RawBitFlags};
33

44
impl<T> fmt::Debug for BitFlags<T>
55
where

enumflags/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ extern crate enumflags2_derive;
9999
#[doc(hidden)]
100100
pub use enumflags2_derive::BitFlags_internal as BitFlags;
101101

102+
/// A trait automatically implemented by `derive(BitFlags)` to make the enum
103+
/// a valid type parameter for `BitFlags<T>`.
104+
pub trait RawBitFlags: Copy + Clone + 'static + _internal::RawBitFlags {}
105+
102106
/// While the module is public, this is only the case because it needs to be
103107
/// accessed by the derive macro. Do not use this directly. Stability guarantees
104108
/// don't apply.
@@ -163,8 +167,6 @@ mod formatting;
163167
mod fallible;
164168
pub use fallible::FromBitsError;
165169

166-
use _internal::RawBitFlags;
167-
168170
/// Represents a set of flags of some type `T`.
169171
/// The type must have the `#[derive(BitFlags)]` attribute applied.
170172
#[derive(Copy, Clone, Eq, Hash)]
@@ -391,7 +393,7 @@ mod impl_serde {
391393
extern crate serde;
392394
use self::serde::{Serialize, Deserialize};
393395
use self::serde::de::{Error, Unexpected};
394-
use super::{BitFlags, _internal::RawBitFlags};
396+
use super::{BitFlags, RawBitFlags};
395397

396398
impl<'a, T> Deserialize<'a> for BitFlags<T>
397399
where

enumflags_derive/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,7 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
241241
concat!("BitFlags<", stringify!(#ident), ">")
242242
}
243243
}
244+
245+
impl ::enumflags2::RawBitFlags for #ident {}
244246
})
245247
}

test_suite/tests/requires_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn format() {
8585

8686
#[test]
8787
fn debug_generic() {
88-
use enumflags2::{BitFlags, _internal::RawBitFlags};
88+
use enumflags2::{BitFlags, RawBitFlags};
8989

9090
#[derive(Debug)]
9191
struct Debug<T: RawBitFlags>(BitFlags<T>);

0 commit comments

Comments
 (0)