Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seal all marker traits and bump version #174

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ against this Rust version.

### Unreleased

### 1.14.0 (2021-09-01)
- [changed] Sealed all marker traits. Documentation already stated that these
should not be implemented outside the crate, so this is not considered a
breaking change.

### 1.13.0 (2021-03-12)
- [changed] MSRV from 1.22.0 to 1.37.0.
- [fixed] `op` macro with 2018 edition import.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typenum"
build = "build/main.rs"
version = "1.13.0" # remember to update html_root_url
version = "1.14.0" # remember to update html_root_url
authors = [
"Paho Lurie-Gregg <[email protected]>",
"Andre Bogus <[email protected]>"
Expand Down
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
)
)]
#![cfg_attr(feature = "cargo-clippy", deny(clippy::missing_inline_in_public_items))]
#![doc(html_root_url = "https://docs.rs/typenum/1.13.0")]
#![doc(html_root_url = "https://docs.rs/typenum/1.14.0")]

// For debugging macros:
// #![feature(trace_macros)]
Expand Down Expand Up @@ -156,3 +156,29 @@ macro_rules! assert_type {
core::marker::PhantomData;
};
}

mod sealed {
use crate::{
ATerm, Bit, Equal, Greater, Less, NInt, NonZero, PInt, TArr, UInt, UTerm, Unsigned, B0, B1,
Z0,
};

pub trait Sealed {}

impl Sealed for B0 {}
impl Sealed for B1 {}

impl Sealed for UTerm {}
impl<U: Unsigned, B: Bit> Sealed for UInt<U, B> {}

impl Sealed for Z0 {}
impl<U: Unsigned + NonZero> Sealed for PInt<U> {}
impl<U: Unsigned + NonZero> Sealed for NInt<U> {}

impl Sealed for Less {}
impl Sealed for Equal {}
impl Sealed for Greater {}

impl Sealed for ATerm {}
impl<V, A> Sealed for TArr<V, A> {}
}
31 changes: 10 additions & 21 deletions src/marker_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@
//! assert_eq!(-42, N42::I32);
//! ```

use crate::sealed::Sealed;

/// A **marker trait** to designate that a type is not zero. All number types in this
/// crate implement `NonZero` except `B0`, `U0`, and `Z0`.
pub trait NonZero {}
pub trait NonZero: Sealed {}

/// A **marker trait** to designate that a type is zero. Only `B0`, `U0`, and `Z0`
/// implement this trait.
pub trait Zero {}
pub trait Zero: Sealed {}

/// A **Marker trait** for the types `Greater`, `Equal`, and `Less`.
///
/// This trait should not be implemented for anything outside this crate.
pub trait Ord {
pub trait Ord: Sealed {
#[allow(missing_docs)]
fn to_ordering() -> ::core::cmp::Ordering;
}

/// The **marker trait** for compile time bits.
///
/// This trait should not be implemented for anything outside this crate.
pub trait Bit: Copy + Default + 'static {
pub trait Bit: Sealed + Copy + Default + 'static {
#[allow(missing_docs)]
const U8: u8;
#[allow(missing_docs)]
Expand All @@ -51,16 +49,14 @@ pub trait Bit: Copy + Default + 'static {

/// The **marker trait** for compile time unsigned integers.
///
/// This trait should not be implemented for anything outside this crate.
///
/// # Example
/// ```rust
/// use typenum::{Unsigned, U3};
///
/// assert_eq!(U3::to_u32(), 3);
/// assert_eq!(U3::I32, 3);
/// ```
pub trait Unsigned: Copy + Default + 'static {
pub trait Unsigned: Sealed + Copy + Default + 'static {
#[allow(missing_docs)]
const U8: u8;
#[allow(missing_docs)]
Expand Down Expand Up @@ -120,16 +116,14 @@ pub trait Unsigned: Copy + Default + 'static {

/// The **marker trait** for compile time signed integers.
///
/// This trait should not be implemented for anything outside this crate.
///
/// # Example
/// ```rust
/// use typenum::{Integer, P3};
///
/// assert_eq!(P3::to_i32(), 3);
/// assert_eq!(P3::I32, 3);
/// ```
pub trait Integer: Copy + Default + 'static {
pub trait Integer: Sealed + Copy + Default + 'static {
#[allow(missing_docs)]
const I8: i8;
#[allow(missing_docs)]
Expand Down Expand Up @@ -161,17 +155,13 @@ pub trait Integer: Copy + Default + 'static {

/// The **marker trait** for type-level arrays of type-level numbers.
///
/// This trait should not be implemented for anything outside this crate.
///
/// Someday, it may contain an associated constant to produce a runtime array,
/// like the other marker traits here. However, that is blocked by [this
/// issue](https://github.com/rust-lang/rust/issues/44168).
pub trait TypeArray {}
pub trait TypeArray: Sealed {}

/// The **marker trait** for type-level numbers which are a power of two.
///
/// This trait should not be implemented for anything outside this crate.
///
/// # Examples
///
/// Here's a working example:
Expand All @@ -196,5 +186,4 @@ pub trait TypeArray {}
/// only_p2::<P511>();
/// only_p2::<P1023>();
/// ```

pub trait PowerOfTwo {}
pub trait PowerOfTwo: Sealed {}