Skip to content

Commit f6d0b28

Browse files
Working around ICE rust-lang/rust#134044 and probably for rust-lang/rust#133199
1 parent 453bd08 commit f6d0b28

File tree

10 files changed

+166
-13
lines changed

10 files changed

+166
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
target/
22
.vscode/settings.json
33
rustc-ice*
4+
tests-nightly/rustc-ice*
5+
tests-nightly/target/

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ unstable_generic_const_own_type = []
3030
# Whether to use nightly #![feature(core_intrinsics)] for core::intrinsics::transmute_unchecked
3131
unstable_transmute_unchecked = []
3232

33-
default = []
33+
#default = []
34+
# For type hinting in VS Code (and similar):
35+
default = ["unstable_generic_const_own_type"]
3436

3537
[package.metadata.docs.rs]
3638
all-features = true

src/amount.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515

1616
use crate::displayer::{DisplayProxy, DisplayerOf};
17+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
1718
use crate::trait_flag::{self, TraitFlags};
1819
use core::cmp::Ordering;
1920
use core::fmt;
@@ -180,12 +181,14 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
180181
/// let n_from_thread = std::thread::spawn(|| &N).join().unwrap();
181182
/// assert_eq!(N, *n_from_thread);
182183
/// ```
184+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
183185
#[repr(transparent)]
184186
pub struct Amount<const TF: TraitFlags, Unit, Repr>(
185187
Repr,
186188
PhantomData<core::sync::atomic::AtomicPtr<Unit>>,
187189
);
188190

191+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
189192
impl<const TF: TraitFlags, Unit, Repr: Copy> Amount<TF, Unit, Repr> {
190193
// @TODO
191194
/// Returns the wrapped value.
@@ -208,6 +211,7 @@ impl<const TF: TraitFlags, Unit, Repr: Copy> Amount<TF, Unit, Repr> {
208211
}
209212
}
210213

214+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
211215
impl<const TF: TraitFlags, Unit, Repr> Amount<TF, Unit, Repr> {
212216
/// `new` is a synonym for `from` that can be evaluated in
213217
/// compile time. The main use-case of this functions is defining
@@ -217,6 +221,7 @@ impl<const TF: TraitFlags, Unit, Repr> Amount<TF, Unit, Repr> {
217221
}
218222
}
219223

224+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
220225
impl<const TF: TraitFlags, Unit: Default, Repr> Amount<TF, Unit, Repr> {
221226
// @TODO similar but without &self
222227
//
@@ -242,6 +247,7 @@ impl<const TF: TraitFlags, Unit: Default, Repr> Amount<TF, Unit, Repr> {
242247
}
243248
}
244249

250+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
245251
impl<const TF: TraitFlags, Unit, Repr> Amount<TF, Unit, Repr>
246252
where
247253
Unit: DisplayerOf<Self>, //@TODO cleanup: Amount<TF, Unit, Repr>>,
@@ -275,6 +281,7 @@ where
275281
}
276282
}
277283

284+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
278285
impl<const TF: TraitFlags, Unit, Repr> From<Repr> for Amount<TF, Unit, Repr> {
279286
fn from(repr: Repr) -> Self {
280287
Self::new(repr)
@@ -287,6 +294,7 @@ impl<const TF: TraitFlags, Unit, Repr> From<Repr> for Amount<TF, Unit, Repr> {
287294
// `PartialEq<Wrapper<T>>` require `T` to implement `PartialEq`, which
288295
// is not what we want: `T` is phantom in our case.
289296

297+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
290298
impl<const TF: TraitFlags, Unit, Repr: Clone> Clone for Amount<TF, Unit, Repr> {
291299
fn clone(&self) -> Self {
292300
Amount(self.0.clone(), PhantomData)
@@ -311,32 +319,38 @@ impl<Unit, Repr: Default> Default
311319
}
312320
}
313321

322+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
314323
impl<const TF: TraitFlags, Unit, Repr: PartialEq> PartialEq for Amount<TF, Unit, Repr> {
315324
fn eq(&self, rhs: &Self) -> bool {
316325
self.0.eq(&rhs.0)
317326
}
318327
}
319328

329+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
320330
impl<const TF: TraitFlags, Unit, Repr: Eq> Eq for Amount<TF, Unit, Repr> {}
321331

332+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
322333
impl<const TF: TraitFlags, Unit, Repr: PartialOrd> PartialOrd for Amount<TF, Unit, Repr> {
323334
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
324335
self.0.partial_cmp(&rhs.0)
325336
}
326337
}
327338

339+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
328340
impl<const TF: TraitFlags, Unit, Repr: Ord> Ord for Amount<TF, Unit, Repr> {
329341
fn cmp(&self, rhs: &Self) -> Ordering {
330342
self.0.cmp(&rhs.0)
331343
}
332344
}
333345

346+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
334347
impl<const TF: TraitFlags, Unit, Repr: Hash> Hash for Amount<TF, Unit, Repr> {
335348
fn hash<H: Hasher>(&self, state: &mut H) {
336349
self.0.hash(state)
337350
}
338351
}
339352

353+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
340354
impl<const TF: TraitFlags, Unit, Repr> Add for Amount<TF, Unit, Repr>
341355
where
342356
Repr: AddAssign + Copy,
@@ -348,6 +362,7 @@ where
348362
}
349363
}
350364

365+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
351366
impl<const TF: TraitFlags, Unit, Repr> AddAssign for Amount<TF, Unit, Repr>
352367
where
353368
Repr: AddAssign + Copy,
@@ -357,6 +372,7 @@ where
357372
}
358373
}
359374

375+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
360376
impl<const TF: TraitFlags, Unit, Repr> SubAssign for Amount<TF, Unit, Repr>
361377
where
362378
Repr: SubAssign + Copy,
@@ -366,6 +382,7 @@ where
366382
}
367383
}
368384

385+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
369386
impl<const TF: TraitFlags, Unit, Repr> Sub for Amount<TF, Unit, Repr>
370387
where
371388
Repr: SubAssign + Copy,
@@ -378,6 +395,7 @@ where
378395
}
379396
}
380397

398+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
381399
impl<const TF: TraitFlags, Unit, Repr> MulAssign<Repr> for Amount<TF, Unit, Repr>
382400
where
383401
Repr: MulAssign + Copy,
@@ -387,6 +405,7 @@ where
387405
}
388406
}
389407

408+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
390409
impl<const TF: TraitFlags, Unit, Repr> Mul<Repr> for Amount<TF, Unit, Repr>
391410
where
392411
Repr: MulAssign + Copy,
@@ -399,6 +418,7 @@ where
399418
}
400419
}
401420

421+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
402422
impl<const TF: TraitFlags, Unit, Repr> Div<Self> for Amount<TF, Unit, Repr>
403423
where
404424
Repr: Div<Repr> + Copy,
@@ -410,6 +430,7 @@ where
410430
}
411431
}
412432

433+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
413434
impl<const TF: TraitFlags, Unit, Repr> fmt::Debug for Amount<TF, Unit, Repr>
414435
where
415436
Repr: fmt::Debug,
@@ -419,6 +440,7 @@ where
419440
}
420441
}
421442

443+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
422444
impl<const TF: TraitFlags, Unit, Repr> fmt::Display for Amount<TF, Unit, Repr>
423445
where
424446
Repr: fmt::Display,
@@ -435,13 +457,15 @@ where
435457
// We want serialization format of `Repr` and the `Amount` to match
436458
// exactly, that's why we have to provide custom instances.
437459
#[cfg(feature = "serde")]
460+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
438461
impl<const TF: TraitFlags, Unit, Repr: Serialize> Serialize for Amount<TF, Unit, Repr> {
439462
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
440463
self.0.serialize(serializer)
441464
}
442465
}
443466

444467
#[cfg(feature = "serde")]
468+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
445469
impl<'de, const TF: TraitFlags, Unit, Repr> Deserialize<'de> for Amount<TF, Unit, Repr>
446470
where
447471
Repr: Deserialize<'de>,

src/id.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515

1616
use crate::displayer::{DisplayProxy, DisplayerOf};
17+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
1718
use crate::trait_flag::{self, TraitFlags};
1819
use core::cmp::Ordering;
1920
use core::fmt;
@@ -162,12 +163,14 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
162163
/// assert_eq!(serde_json::to_string(&user_id).unwrap(), serde_json::to_string(&repr).unwrap());
163164
/// }
164165
/// ```
166+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
165167
#[repr(transparent)]
166168
pub struct Id<const TF: TraitFlags, Entity, Repr>(
167169
Repr,
168170
PhantomData<core::sync::atomic::AtomicPtr<Entity>>,
169171
);
170172

173+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
171174
impl<const TF: TraitFlags, Entity, Repr> Id<TF, Entity, Repr> {
172175
/// `get` returns the underlying representation of the identifier.
173176
///
@@ -211,11 +214,12 @@ impl<const TF: TraitFlags, Entity, Repr> Id<TF, Entity, Repr> {
211214
}
212215
}
213216

217+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
214218
impl<const TF: TraitFlags, Entity, Repr> Id<TF, Entity, Repr>
215219
where
216-
Entity: DisplayerOf<Self>, //TODO rmeove: DisplayerOf<Id<TF, Entity, Repr>>,
220+
Entity: DisplayerOf<Self>,
217221
{
218-
/// `display` provides a machanism to implement a custom display
222+
/// `display` provides a mechanism to implement a custom display
219223
/// for phantom types.
220224
///
221225
/// ```
@@ -224,29 +228,34 @@ where
224228
/// feature(generic_const_exprs),
225229
/// )]
226230
///
227-
/// //use phantom_newtype::{Id, DisplayerOf};
228231
/// use phantom_newtype::DisplayerOf;
229-
/// use phantom_newtype::IdNoCopyNoDefault;
230-
/// //use phantom_newtype::IdIsCopyIsDefault as Id;
231-
/// //use phantom_newtype::IdNoCopyNoDefault as Id;
232232
/// use core::fmt;
233233
///
234234
/// enum Message {}
235-
/// type MessageId = IdNoCopyNoDefault<Message, [u8; 32]>;
235+
/// // This causes ICE:
236+
/// //type MessageId = phantom_newtype::Id<Message, [u8; 32]>;
237+
/// // No ICE:
238+
/// type MessageId = phantom_newtype::IdForFlags<{phantom_newtype::trait_flag::TRAIT_FLAGS_NO_COPY_NO_DEFAULT}, Message, [u8; 32]>;
236239
///
237240
/// impl DisplayerOf<MessageId> for Message {
238241
/// fn display(id: &MessageId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
239-
/// todo!()
242+
/// id.get().iter().try_for_each(|b| write!(f, "{:02x}", b))
240243
/// }
241244
/// }
242245
///
243-
/// MessageId::from([0u8; 32]);
246+
/// let vec: Vec<_> = (0u8..32u8).collect();
247+
/// let mut arr: [u8; 32] = [0u8; 32];
248+
/// (&mut arr[..]).copy_from_slice(&vec[..]);
249+
///
250+
/// assert_eq!(format!("{}", MessageId::from(arr).display()),
251+
/// "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
244252
/// ```
245253
pub fn display(&self) -> DisplayProxy<'_, Self, Entity> {
246254
DisplayProxy::new(self)
247255
}
248256
}
249257

258+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
250259
impl<const TF: TraitFlags, Entity, Repr: Clone> Clone for Id<TF, Entity, Repr> {
251260
fn clone(&self) -> Self {
252261
Self::from(self.get().clone())
@@ -271,51 +280,60 @@ impl<Unit, Repr: Default> Default
271280
}
272281
}
273282

283+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
274284
impl<const TF: TraitFlags, Entity, Repr: PartialEq> PartialEq for Id<TF, Entity, Repr> {
275285
fn eq(&self, rhs: &Self) -> bool {
276286
self.get().eq(&rhs.get())
277287
}
278288
}
279289

290+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
280291
impl<const TF: TraitFlags, Entity, Repr: PartialOrd> PartialOrd for Id<TF, Entity, Repr> {
281292
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
282293
self.get().partial_cmp(&rhs.get())
283294
}
284295
}
285296

297+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
286298
impl<const TF: TraitFlags, Entity, Repr: Ord> Ord for Id<TF, Entity, Repr> {
287299
fn cmp(&self, rhs: &Self) -> Ordering {
288300
self.get().cmp(&rhs.get())
289301
}
290302
}
291303

304+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
292305
impl<const TF: TraitFlags, Entity, Repr: Hash> Hash for Id<TF, Entity, Repr> {
293306
fn hash<H: Hasher>(&self, state: &mut H) {
294307
self.get().hash(state)
295308
}
296309
}
297310

311+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
298312
impl<const TF: TraitFlags, Entity, Repr> From<Repr> for Id<TF, Entity, Repr> {
299313
fn from(repr: Repr) -> Self {
300314
Self::new(repr)
301315
}
302316
}
303317

318+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
304319
impl<const TF: TraitFlags, Entity, Repr: Eq> Eq for Id<TF, Entity, Repr> {}
305320

321+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
306322
impl<const TF: TraitFlags, Entity, Repr: fmt::Debug> fmt::Debug for Id<TF, Entity, Repr> {
307323
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
308324
write!(f, "{:?}", self.get())
309325
}
310326
}
311327

328+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
312329
impl<const TF: TraitFlags, Entity, Repr: fmt::Display> fmt::Display for Id<TF, Entity, Repr> {
313330
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314331
write!(f, "{}", self.get())
315332
}
316333
}
317334

318335
#[cfg(feature = "serde")]
336+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
319337
impl<const TF: TraitFlags, Entity, Repr> Serialize for Id<TF, Entity, Repr>
320338
where
321339
Repr: Serialize,
@@ -326,6 +344,7 @@ where
326344
}
327345

328346
#[cfg(feature = "serde")]
347+
#[cfg_attr(feature = "unstable_generic_const_own_type", allow(deprecated))]
329348
impl<'de, const TF: TraitFlags, Entity, Repr> Deserialize<'de> for Id<TF, Entity, Repr>
330349
where
331350
Repr: Deserialize<'de>,

0 commit comments

Comments
 (0)