Skip to main content

design/
cell.rs

1use std::{
2    cell::{
3        Cell,
4        RefMut,
5        SyncUnsafeCell,
6        UnsafeCell,
7    },
8    marker::PhantomData,
9};
10
11use crate::{
12    ops::place::{
13        BorrowPlace,
14        CreateHandle,
15        PlaceHandle,
16        PlaceProxy,
17        PlaceWrapper,
18        WrapPlace,
19        borrowck::{
20            AccessKind,
21            Instant,
22            Lifetime,
23        },
24    },
25    place::{
26        Subplace,
27        TransmutedSubplace,
28    },
29};
30
31impl<T: ?Sized> PlaceWrapper for Cell<T> {
32    type Inner = T;
33}
34
35unsafe impl<S> WrapPlace<S> for Cell<S::Source>
36where
37    S: Subplace,
38{
39    type Wrapped = TransmutedSubplace<S, Cell<S::Source>, Cell<S::Target>>;
40
41    fn wrap(subplace: S) -> Self::Wrapped {
42        unsafe { TransmutedSubplace::new_unchecked(subplace) }
43    }
44}
45
46impl<T: ?Sized> PlaceWrapper for UnsafeCell<T> {
47    type Inner = T;
48}
49
50unsafe impl<S> WrapPlace<S> for UnsafeCell<S::Source>
51where
52    S: Subplace,
53{
54    type Wrapped =
55        TransmutedSubplace<S, UnsafeCell<S::Source>, UnsafeCell<S::Target>>;
56
57    fn wrap(subplace: S) -> Self::Wrapped {
58        unsafe { TransmutedSubplace::new_unchecked(subplace) }
59    }
60}
61
62impl<T: ?Sized> PlaceWrapper for SyncUnsafeCell<T> {
63    type Inner = T;
64}
65
66unsafe impl<S> WrapPlace<S> for SyncUnsafeCell<S::Source>
67where
68    S: Subplace,
69{
70    type Wrapped = TransmutedSubplace<
71        S,
72        SyncUnsafeCell<S::Source>,
73        SyncUnsafeCell<S::Target>,
74    >;
75
76    fn wrap(subplace: S) -> Self::Wrapped {
77        unsafe { TransmutedSubplace::new_unchecked(subplace) }
78    }
79}
80
81pub struct CellMutHandle<'b, T> {
82    phantom: PhantomData<&'b mut T>,
83}
84
85impl<'b, T> PlaceProxy for RefMut<'b, T> {
86    type Target = T;
87}
88
89unsafe impl<'b, T> CreateHandle<Instant> for RefMut<'b, T> {
90    type Handle = CellMutHandle<'b, T>;
91
92    const ACCESS: AccessKind = AccessKind::Shared;
93
94    unsafe fn handle_from_raw(_this: *const Self) -> Self::Handle {
95        CellMutHandle { phantom: PhantomData }
96    }
97}
98
99impl<T> PlaceHandle for CellMutHandle<'_, T> {
100    type Target = T;
101}
102
103unsafe impl<'a, 'b, T> BorrowPlace<&'a mut T> for CellMutHandle<'b, T>
104where
105    'b: 'a,
106{
107    const ACCESS: AccessKind = AccessKind::Exclusive;
108    type Timing = Lifetime<'a>;
109    const SAFE: bool = true;
110
111    unsafe fn borrow(self) -> &'a mut T {
112        ::core::panicking::panic("not yet implemented")todo!()
113    }
114}
115
116unsafe impl<'a, 'b, T> BorrowPlace<RefMut<'a, T>> for CellMutHandle<'b, T>
117where
118    'b: 'a,
119{
120    const ACCESS: AccessKind = AccessKind::Exclusive;
121    /// Importantly, this is `'b` and not `'a`!
122    type Timing = Lifetime<'b>;
123    const SAFE: bool = true;
124
125    unsafe fn borrow(self) -> RefMut<'a, T> {
126        ::core::panicking::panic("not yet implemented")todo!()
127    }
128}