Skip to content

Commit

Permalink
Adjusting tests for rust-lang/rust#134044 and probably for rust-lang/…
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lyons-kehl committed Dec 10, 2024
1 parent 2caa986 commit 453bd08
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
.vscode/settings.json
rustc-ice*
53 changes: 52 additions & 1 deletion src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// amounts more convenient. For example, you can compare amounts:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// enum Apples {}
Expand All @@ -63,6 +68,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// You can do simple arithmetics with amounts:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// enum Apples {}
Expand All @@ -80,6 +90,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// scalar or divide amounts:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// enum Apples {}
Expand All @@ -94,6 +109,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// `Amount` instead of `u64` doesn't incur any runtime penalty:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// enum Meters {}
Expand All @@ -106,6 +126,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// forms of `Amount<Unit, Repr>` and `Repr` are identical.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// #[cfg(feature = "serde")] {
/// use phantom_newtype::Amount;
/// use serde::{Serialize, Deserialize};
Expand All @@ -124,6 +149,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// You can also declare constants of `Amount<Unit, Repr>` using `new`
/// function:
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
/// enum Meters {}
/// type Distance = Amount<Meters, u64>;
Expand All @@ -136,6 +166,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// matter which `Unit` is used.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// type Cell = core::cell::RefCell<i64>;
Expand All @@ -156,6 +191,11 @@ impl<const TF: TraitFlags, Unit, Repr: Copy> Amount<TF, Unit, Repr> {
/// Returns the wrapped value.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// enum Apples {}
Expand Down Expand Up @@ -184,6 +224,11 @@ impl<const TF: TraitFlags, Unit: Default, Repr> Amount<TF, Unit, Repr> {
/// they implement the `Default` trait:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Amount;
///
/// #[derive(Debug, Default)]
Expand All @@ -204,7 +249,13 @@ where
/// `display` provides a machanism to implement a custom display
/// for phantom types.
///
/// ```
/// ```ignore
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs),
/// feature(adt_const_params),
/// )]
///
/// use phantom_newtype::{Amount, DisplayerOf};
/// use core::fmt;
///
Expand Down
57 changes: 48 additions & 9 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// `Id` is cheap to copy if `Repr` is:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Id;
///
/// enum Message {}
Expand All @@ -75,6 +80,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// this property:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Id;
/// use std::collections::HashMap;
///
Expand All @@ -94,6 +104,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// semantic value in comparing ids.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use std::collections::BTreeMap;
/// use phantom_newtype::Id;
///
Expand All @@ -112,6 +127,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// matter which `Entity` is used.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Id;
///
/// type Cell = core::cell::RefCell<i64>;
Expand All @@ -126,6 +146,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// forms of `Id<Entity, Repr>` and `Repr` are identical.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// #[cfg(feature = "serde")] {
/// use phantom_newtype::Id;
/// use serde::{Serialize, Deserialize};
Expand All @@ -147,6 +172,11 @@ impl<const TF: TraitFlags, Entity, Repr> Id<TF, Entity, Repr> {
/// `get` returns the underlying representation of the identifier.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Id;
///
/// enum User {}
Expand All @@ -163,6 +193,11 @@ impl<const TF: TraitFlags, Entity, Repr> Id<TF, Entity, Repr> {
/// constants:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Id;
/// enum User {}
/// type UserId = Id<User, u64>;
Expand All @@ -184,24 +219,28 @@ where
/// for phantom types.
///
/// ```
/// use phantom_newtype::{Id, DisplayerOf};
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs),
/// )]
///
/// //use phantom_newtype::{Id, DisplayerOf};
/// use phantom_newtype::DisplayerOf;
/// use phantom_newtype::IdNoCopyNoDefault;
/// //use phantom_newtype::IdIsCopyIsDefault as Id;
/// //use phantom_newtype::IdNoCopyNoDefault as Id;
/// use core::fmt;
///
/// enum Message {}
/// type MessageId = Id<Message, [u8; 32]>;
/// type MessageId = IdNoCopyNoDefault<Message, [u8; 32]>;
///
/// impl DisplayerOf<MessageId> for Message {
/// fn display(id: &MessageId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// id.get().iter().try_for_each(|b| write!(f, "{:02x}", b))
/// todo!()
/// }
/// }
///
/// let vec: Vec<_> = (0u8..32u8).collect();
/// let mut arr: [u8; 32] = [0u8; 32];
/// (&mut arr[..]).copy_from_slice(&vec[..]);
///
/// assert_eq!(format!("{}", MessageId::from(arr).display()),
/// "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
/// MessageId::from([0u8; 32]);
/// ```
pub fn display(&self) -> DisplayProxy<'_, Self, Entity> {
DisplayProxy::new(self)
Expand Down
44 changes: 42 additions & 2 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// You can compare instants:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// enum SecondsFromEpoch {}
Expand All @@ -51,6 +56,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// * Add/subtract amount of units to/from an instant to get another instant.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::{Amount, Instant};
///
/// enum SecondsFromEpoch {}
Expand All @@ -71,6 +81,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// can scale them by a scalar or divide to get a scalar back:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// enum SecondsFromEpoch {}
Expand All @@ -86,6 +101,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// `Instant` instead of `u64` doesn't incur any runtime penalty:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// enum SecondsFromEpoch {}
Expand All @@ -97,7 +117,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// Instants can be serialized and deserialized with `serde`. Serialized
/// forms of `Instant<Unit, Repr>` and `Repr` are identical.
///
/// ```
/// ```ignore
/// #[cfg(feature = "serde")] {
/// use phantom_newtype::Instant;
/// use serde::{Serialize, Deserialize};
Expand All @@ -118,6 +138,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// You can also declare constants of `Instant<Unit, Repr>` using `new`
/// function:
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// enum SecondsFromEpoch {}
Expand All @@ -130,6 +155,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// matter which `Unit` is used.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// type Cell = core::cell::RefCell<i64>;
Expand All @@ -153,6 +183,11 @@ impl<const TF: TraitFlags, Unit, Repr: Copy> Instant<TF, Unit, Repr> {
/// Returns the wrapped value.
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// enum Apples {}
Expand All @@ -179,6 +214,11 @@ impl<const TF: TraitFlags, Unit: Default, Repr> Instant<TF, Unit, Repr> {
/// they implement the `Default` trait:
///
/// ```
/// #![cfg_attr(
/// feature = "unstable_generic_const_own_type",
/// feature(generic_const_exprs)
/// )]
///
/// use phantom_newtype::Instant;
///
/// #[derive(Debug, Default)]
Expand All @@ -199,7 +239,7 @@ where
/// `display` provides a machanism to implement a custom display
/// for phantom types.
///
/// ```
/// ```ignore
/// use phantom_newtype::{Instant, DisplayerOf};
/// use core::fmt;
///
Expand Down
Loading

0 comments on commit 453bd08

Please sign in to comment.