Skip to content

Commit

Permalink
Expose "the" RawBitFlags trait in the public API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
meithecatte committed Apr 21, 2020
1 parent e2d2f68 commit 5f7eff0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion enumflags/src/fallible.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion enumflags/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt::{self, Debug, Binary};
use crate::{BitFlags, _internal::RawBitFlags};
use crate::{BitFlags, RawBitFlags};

impl<T> fmt::Debug for BitFlags<T>
where
Expand Down
8 changes: 5 additions & 3 deletions enumflags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`.
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.
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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<T>
where
Expand Down
2 changes: 2 additions & 0 deletions enumflags_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,7 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
concat!("BitFlags<", stringify!(#ident), ">")
}
}

impl ::enumflags2::RawBitFlags for #ident {}
})
}
2 changes: 1 addition & 1 deletion test_suite/tests/requires_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn format() {

#[test]
fn debug_generic() {
use enumflags2::{BitFlags, _internal::RawBitFlags};
use enumflags2::{BitFlags, RawBitFlags};

#[derive(Debug)]
struct Debug<T: RawBitFlags>(BitFlags<T>);
Expand Down

0 comments on commit 5f7eff0

Please sign in to comment.