13
13
//! Invariants are encoded as ([`Aliasing`], [`Alignment`], [`Validity`])
14
14
//! triples implementing the [`Invariants`] trait.
15
15
16
- use super :: * ;
17
-
18
16
/// The invariants of a [`Ptr`][super::Ptr].
19
17
pub trait Invariants : Sealed {
20
18
type Aliasing : Aliasing ;
@@ -82,6 +80,15 @@ pub trait Aliasing:
82
80
/// Aliasing>::Variance<'a, T>` to inherit this variance.
83
81
#[ doc( hidden) ]
84
82
type Variance < ' a , T : ' a + ?Sized > ;
83
+
84
+ // #[doc(hidden)]
85
+ // type Applied<'a, T: 'a + ?Sized>;
86
+
87
+ // #[doc(hidden)]
88
+ // fn into_ptr<'a, T: 'a + ?Sized>(ptr: Self::Applied<'a, T>) -> PtrInner<'a, T>;
89
+
90
+ // #[doc(hidden)]
91
+ // unsafe fn from_ptr<'a, T: 'a + ?Sized>(ptr: PtrInner<'a, T>) -> Self::Applied<'a, T>;
85
92
}
86
93
87
94
#[ doc( hidden) ]
@@ -136,14 +143,7 @@ impl<
136
143
///
137
144
/// Given `A: Reference`, callers may assume that either `A = Shared` or `A =
138
145
/// Exclusive`.
139
- pub trait Reference : Aliasing + Sealed {
140
- fn with < ' a , T , I , S , E , O > ( ptr : Ptr < ' a , T , I > , shared : S , exclusive : E ) -> O
141
- where
142
- T : ' a + ?Sized ,
143
- I : Invariants < Aliasing = Self > ,
144
- S : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Shared > > ) -> O ,
145
- E : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Exclusive > > ) -> O ;
146
- }
146
+ pub trait Reference : ReadFoo + Sealed { }
147
147
148
148
/// It is unknown whether any invariant holds.
149
149
pub enum Unknown { }
@@ -189,18 +189,7 @@ impl Aliasing for Shared {
189
189
const IS_EXCLUSIVE : bool = false ;
190
190
type Variance < ' a , T : ' a + ?Sized > = & ' a T ;
191
191
}
192
- impl Reference for Shared {
193
- #[ inline( always) ]
194
- fn with < ' a , T , I , S , E , O > ( ptr : Ptr < ' a , T , I > , shared : S , _exclusive : E ) -> O
195
- where
196
- T : ' a + ?Sized ,
197
- I : Invariants < Aliasing = Shared > ,
198
- S : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Shared > > ) -> O ,
199
- E : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Exclusive > > ) -> O ,
200
- {
201
- shared ( ptr. unify_invariants ( ) )
202
- }
203
- }
192
+ impl Reference for Shared { }
204
193
205
194
/// The `Ptr<'a, T>` adheres to the aliasing rules of a `&'a mut T`.
206
195
///
@@ -215,16 +204,41 @@ impl Aliasing for Exclusive {
215
204
const IS_EXCLUSIVE : bool = true ;
216
205
type Variance < ' a , T : ' a + ?Sized > = & ' a mut T ;
217
206
}
218
- impl Reference for Exclusive {
219
- #[ inline( always) ]
220
- fn with < ' a , T , I , S , E , O > ( ptr : Ptr < ' a , T , I > , _shared : S , exclusive : E ) -> O
221
- where
222
- T : ' a + ?Sized ,
223
- I : Invariants < Aliasing = Exclusive > ,
224
- S : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Shared > > ) -> O ,
225
- E : FnOnce ( Ptr < ' a , T , I :: WithAliasing < Exclusive > > ) -> O ,
226
- {
227
- exclusive ( ptr. unify_invariants ( ) )
207
+ impl Reference for Exclusive { }
208
+
209
+ #[ cfg( feature = "alloc" ) ]
210
+ pub use _alloc:: * ;
211
+ #[ cfg( feature = "alloc" ) ]
212
+ mod _alloc {
213
+ use alloc:: boxed:: Box as Bx ;
214
+
215
+ use super :: * ;
216
+
217
+ pub enum Box { }
218
+ impl AliasingInner for Box {
219
+ type MappedTo < M : AliasingMapping > = M :: FromBox ;
220
+ }
221
+ impl Aliasing for Box {
222
+ const IS_EXCLUSIVE : bool = true ;
223
+ type Variance < ' a , T : ' a + ?Sized > = Bx < T > ;
224
+ }
225
+ }
226
+
227
+ #[ cfg( feature = "std" ) ]
228
+ pub use _std:: * ;
229
+ #[ cfg( feature = "std" ) ]
230
+ mod _std {
231
+ use std:: sync:: Arc as Ac ;
232
+
233
+ use super :: * ;
234
+
235
+ pub enum Arc { }
236
+ impl AliasingInner for Arc {
237
+ type MappedTo < M : AliasingMapping > = M :: FromArc ;
238
+ }
239
+ impl Aliasing for Arc {
240
+ const IS_EXCLUSIVE : bool = true ;
241
+ type Variance < ' a , T : ' a + ?Sized > = Ac < T > ;
228
242
}
229
243
}
230
244
@@ -279,6 +293,27 @@ impl ValidityInner for Valid {
279
293
type MappedTo < M : ValidityMapping > = M :: FromValid ;
280
294
}
281
295
296
+ // Aliasing modes that permit reading at all (ie, everything but Inaccessible).
297
+ pub trait ReadFoo : Aliasing { }
298
+ impl ReadFoo for Shared { }
299
+ impl ReadFoo for Exclusive { }
300
+ #[ cfg( feature = "alloc" ) ]
301
+ impl ReadFoo for Box { }
302
+ #[ cfg( feature = "std" ) ]
303
+ impl ReadFoo for Arc { }
304
+
305
+ // Shared, Arc, etc
306
+ pub trait SharedFoo : Aliasing { }
307
+ impl SharedFoo for Shared { }
308
+ #[ cfg( feature = "std" ) ]
309
+ impl SharedFoo for Arc { }
310
+
311
+ // Exclusive, Box, etc
312
+ pub trait ExclusiveFoo : Aliasing { }
313
+ impl ExclusiveFoo for Exclusive { }
314
+ #[ cfg( feature = "alloc" ) ]
315
+ impl ExclusiveFoo for Box { }
316
+
282
317
/// [`Ptr`](crate::Ptr) referents that permit unsynchronized read operations.
283
318
///
284
319
/// `T: Read<A, R>` implies that a pointer to `T` with aliasing `A` permits
@@ -295,8 +330,8 @@ impl ValidityInner for Valid {
295
330
/// permitted to perform unsynchronized reads from its referent.
296
331
pub trait Read < A : Aliasing , R : ReadReason > { }
297
332
298
- impl < A : Reference , T : ?Sized + crate :: Immutable > Read < A , BecauseImmutable > for T { }
299
- impl < T : ?Sized > Read < Exclusive , BecauseExclusive > for T { }
333
+ impl < A : ReadFoo , T : ?Sized + crate :: Immutable > Read < A , BecauseImmutable > for T { }
334
+ impl < A : ExclusiveFoo , T : ?Sized > Read < A , BecauseExclusive > for T { }
300
335
301
336
/// Used to disambiguate [`Read`] impls.
302
337
pub trait ReadReason : Sealed { }
@@ -326,6 +361,10 @@ mod sealed {
326
361
impl Sealed for Inaccessible { }
327
362
impl Sealed for Shared { }
328
363
impl Sealed for Exclusive { }
364
+ #[ cfg( feature = "alloc" ) ]
365
+ impl Sealed for Box { }
366
+ #[ cfg( feature = "std" ) ]
367
+ impl Sealed for Arc { }
329
368
330
369
impl Sealed for Aligned { }
331
370
@@ -389,6 +428,10 @@ mod mapping {
389
428
type FromInaccessible : Aliasing ;
390
429
type FromShared : Aliasing ;
391
430
type FromExclusive : Aliasing ;
431
+ #[ cfg( feature = "alloc" ) ]
432
+ type FromBox : Aliasing ;
433
+ #[ cfg( feature = "std" ) ]
434
+ type FromArc : Aliasing ;
392
435
}
393
436
394
437
/// A mapping from one [`Alignment`] type to another.
@@ -457,12 +500,20 @@ mod mapping {
457
500
type FromInaccessible = FromInaccessible ;
458
501
type FromShared = FromShared ;
459
502
type FromExclusive = FromExclusive ;
503
+ #[ cfg( feature = "alloc" ) ]
504
+ type FromBox = Inaccessible ;
505
+ #[ cfg( feature = "std" ) ]
506
+ type FromArc = Inaccessible ;
460
507
}
461
508
462
509
impl AliasingMapping for Inaccessible {
463
510
type FromInaccessible = Inaccessible ;
464
511
type FromShared = Inaccessible ;
465
512
type FromExclusive = Inaccessible ;
513
+ #[ cfg( feature = "alloc" ) ]
514
+ type FromBox = Inaccessible ;
515
+ #[ cfg( feature = "std" ) ]
516
+ type FromArc = Inaccessible ;
466
517
}
467
518
468
519
pub enum UnsafeCellMismatch { }
@@ -471,24 +522,40 @@ mod mapping {
471
522
type FromInaccessible = Inaccessible ;
472
523
type FromShared = Inaccessible ;
473
524
type FromExclusive = Exclusive ;
525
+ #[ cfg( feature = "alloc" ) ]
526
+ type FromBox = Box ;
527
+ #[ cfg( feature = "std" ) ]
528
+ type FromArc = Inaccessible ;
474
529
}
475
530
476
531
impl AliasingMapping for Preserved {
477
532
type FromInaccessible = Inaccessible ;
478
533
type FromShared = Shared ;
479
534
type FromExclusive = Exclusive ;
535
+ #[ cfg( feature = "alloc" ) ]
536
+ type FromBox = Box ;
537
+ #[ cfg( feature = "std" ) ]
538
+ type FromArc = Arc ;
480
539
}
481
540
482
541
impl AliasingMapping for Shared {
483
542
type FromInaccessible = Shared ;
484
543
type FromShared = Shared ;
485
544
type FromExclusive = Shared ;
545
+ #[ cfg( feature = "alloc" ) ]
546
+ type FromBox = Shared ;
547
+ #[ cfg( feature = "std" ) ]
548
+ type FromArc = Shared ;
486
549
}
487
550
488
551
impl AliasingMapping for Exclusive {
489
552
type FromInaccessible = Exclusive ;
490
553
type FromShared = Exclusive ;
491
554
type FromExclusive = Exclusive ;
555
+ #[ cfg( feature = "alloc" ) ]
556
+ type FromBox = Exclusive ;
557
+ #[ cfg( feature = "std" ) ]
558
+ type FromArc = Exclusive ;
492
559
}
493
560
494
561
impl < FromUnknown : Alignment , FromAligned : Alignment > AlignmentMapping
0 commit comments