Skip to main content

core/ops/
place.rs

1#![expect(missing_docs, reason = "WIP")]
2
3use crate::marker::PhantomData;
4use crate::ptr::{NonNull, Pointee};
5
6#[unstable(feature = "field_projections", issue = "145383")]
7pub trait PlaceProxy {
8    #[unstable(feature = "field_projections", issue = "145383")]
9    type Target: ?Sized;
10}
11
12#[unstable(feature = "field_projections", issue = "145383")]
13pub trait PlaceHandle: Sized {
14    #[unstable(feature = "field_projections", issue = "145383")]
15    type Target: ?Sized;
16}
17
18#[unstable(feature = "field_projections", issue = "145383")]
19#[lang = "deref_place"]
20pub unsafe trait DerefPlace: PlaceHandle
21where
22    Self::Target: PlaceProxy,
23{
24    #[unstable(feature = "field_projections", issue = "145383")]
25    #[lang = "deref_place_pointee_handle"]
26    type PointeeHandle: PlaceHandle<Target = <Self::Target as PlaceProxy>::Target>;
27
28    #[unstable(feature = "field_projections", issue = "145383")]
29    #[lang = "deref_place_deref_place"]
30    unsafe fn deref_place(self) -> Self::PointeeHandle;
31}
32
33#[unstable(feature = "field_projections", issue = "145383")]
34#[lang = "project_place"]
35pub unsafe trait ProjectPlace<S>: PlaceHandle
36where
37    S: Subplace<Source = Self::Target>,
38{
39    #[unstable(feature = "field_projections", issue = "145383")]
40    #[lang = "project_place_projected"]
41    type Projected: PlaceHandle<Target = S::Target>;
42
43    #[unstable(feature = "field_projections", issue = "145383")]
44    #[lang = "project_place_project_place"]
45    unsafe fn project_place(self, subplace: S) -> Self::Projected;
46}
47
48#[unstable(feature = "field_projections", issue = "145383")]
49pub unsafe trait Subplace: Sized {
50    #[unstable(feature = "field_projections", issue = "145383")]
51    type Source: ?Sized;
52    #[unstable(feature = "field_projections", issue = "145383")]
53    type Target: ?Sized;
54
55    #[unstable(feature = "field_projections", issue = "145383")]
56    fn offset(
57        self,
58        metadata: <Self::Source as Pointee>::Metadata,
59    ) -> (usize, <Self::Target as Pointee>::Metadata);
60}
61
62use crate::field::Field;
63
64#[unstable(feature = "field_projections", issue = "145383")]
65unsafe impl<F: Field> Subplace for F {
66    type Source = F::Base;
67    type Target = F::Type;
68
69    fn offset(
70        self,
71        _metadata: <Self::Source as Pointee>::Metadata,
72    ) -> (usize, <Self::Target as Pointee>::Metadata) {
73        (F::OFFSET, ())
74    }
75}
76
77/// Primitive handle referring to a local variable or memory location.
78///
79/// # Design Tradeoff & Intermediate Status
80///
81/// `LocalHandle` is an intermediate concrete handle introduced to enable developing,
82/// testing, and validating place projections (`ProjectPlace`) and subplaces (`Subplace`)
83/// prior to full compiler-native place support.
84///
85/// In the full Field Projections design:
86/// - Place capabilities and lifetimes are statically partitioned into dedicated handle
87///   types (e.g. `MutHandle<'a, T>` and `RefHandle<'a, T>`).
88/// - Access permissions are checked by the borrow checker through terminal operation
89///   traits (`BorrowPlace`, `WritePlace`, `ReadPlace`).
90///
91/// In this intermediate milestone:
92/// - `LocalHandle<T>` acts as a unified concrete handle storing a `*mut T`.
93/// - Shared versus exclusive access permissions are not separated into distinct types;
94///   callers must uphold `unsafe` preconditions when obtaining or mutating through raw
95///   pointers via [`as_mut_ptr`](Self::as_mut_ptr).
96#[lang = "local_handle"]
97#[unstable(feature = "field_projections", issue = "145383")]
98pub struct LocalHandle<T: ?Sized> {
99    ptr: *mut T,
100}
101
102/// Creates a raw place handle to a place expression.
103#[unstable(feature = "field_projections", issue = "145383")]
104#[allow_internal_unstable(internal_syntax)]
105#[rustc_macro_transparency = "semiopaque"]
106pub macro raw_handle($place:expr) {
107    k#raw_handle($place)
108}
109
110#[unstable(feature = "field_projections", issue = "145383")]
111impl<T: ?Sized> Clone for LocalHandle<T> {
112    fn clone(&self) -> Self {
113        *self
114    }
115}
116
117#[unstable(feature = "field_projections", issue = "145383")]
118impl<T: ?Sized> Copy for LocalHandle<T> {}
119
120#[unstable(feature = "field_projections", issue = "145383")]
121impl<T: ?Sized> crate::fmt::Debug for LocalHandle<T> {
122    fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
123        f.debug_struct("LocalHandle").field("ptr", &self.ptr).finish()
124    }
125}
126
127impl<T: ?Sized> LocalHandle<T> {
128    /// Obtains a const raw pointer to the place.
129    #[unstable(feature = "field_projections", issue = "145383")]
130    pub const fn as_ptr(&self) -> *const T {
131        self.ptr
132    }
133
134    /// Obtains a mutable raw pointer to the place.
135    #[unstable(feature = "field_projections", issue = "145383")]
136    pub const fn as_mut_ptr(&self) -> *mut T {
137        self.ptr
138    }
139}
140
141#[unstable(feature = "field_projections", issue = "145383")]
142impl<T: ?Sized> PlaceHandle for LocalHandle<T> {
143    type Target = T;
144}
145
146#[unstable(feature = "field_projections", issue = "145383")]
147unsafe impl<S> ProjectPlace<S> for LocalHandle<S::Source>
148where
149    S: Subplace,
150    S::Source: Sized,
151    S::Target: Sized,
152{
153    type Projected = LocalHandle<S::Target>;
154
155    unsafe fn project_place(self, subplace: S) -> Self::Projected {
156        let (offset, _) = subplace.offset(());
157        // SAFETY: `subplace` guarantees that `offset` produces a valid byte offset
158        // within the allocation of `self`.
159        let new_ptr = unsafe { (self.ptr as *mut u8).add(offset) as *mut S::Target };
160        LocalHandle { ptr: new_ptr }
161    }
162}
163
164#[unstable(feature = "field_projections", issue = "145383")]
165impl<T: ?Sized> PlaceProxy for &T {
166    type Target = T;
167}
168
169#[unstable(feature = "field_projections", issue = "145383")]
170impl<T: ?Sized> PlaceProxy for &mut T {
171    type Target = T;
172}
173
174#[unstable(feature = "field_projections", issue = "145383")]
175impl<T: ?Sized> PlaceProxy for *const T {
176    type Target = T;
177}
178
179#[unstable(feature = "field_projections", issue = "145383")]
180impl<T: ?Sized> PlaceProxy for *mut T {
181    type Target = T;
182}
183
184/// Handle referring to a place behind a shared reference.
185#[unstable(feature = "field_projections", issue = "145383")]
186pub struct RefHandle<'a, T: ?Sized> {
187    ptr: NonNull<T>,
188    _lt: PhantomData<&'a T>,
189}
190
191#[unstable(feature = "field_projections", issue = "145383")]
192impl<'a, T: ?Sized> Clone for RefHandle<'a, T> {
193    fn clone(&self) -> Self {
194        *self
195    }
196}
197
198#[unstable(feature = "field_projections", issue = "145383")]
199impl<'a, T: ?Sized> Copy for RefHandle<'a, T> {}
200
201#[unstable(feature = "field_projections", issue = "145383")]
202impl<'a, T: ?Sized> crate::fmt::Debug for RefHandle<'a, T> {
203    fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
204        f.debug_struct("RefHandle").field("ptr", &self.ptr).finish()
205    }
206}
207
208impl<'a, T: ?Sized> RefHandle<'a, T> {
209    /// Obtains a const raw pointer to the place.
210    #[unstable(feature = "field_projections", issue = "145383")]
211    pub const fn as_ptr(&self) -> *const T {
212        self.ptr.as_ptr()
213    }
214
215    /// Obtains a shared reference to the place.
216    #[unstable(feature = "field_projections", issue = "145383")]
217    pub const unsafe fn as_ref(&self) -> &'a T {
218        // SAFETY: Guaranteed by the caller to be a valid dereferenceable shared reference.
219        unsafe { self.ptr.as_ref() }
220    }
221}
222
223#[unstable(feature = "field_projections", issue = "145383")]
224impl<'a, T: ?Sized> PlaceHandle for RefHandle<'a, T> {
225    type Target = T;
226}
227
228#[unstable(feature = "field_projections", issue = "145383")]
229unsafe impl<'a, S> ProjectPlace<S> for RefHandle<'a, S::Source>
230where
231    S: Subplace,
232    S::Source: Sized,
233    S::Target: Sized + 'a,
234{
235    type Projected = RefHandle<'a, S::Target>;
236
237    unsafe fn project_place(self, subplace: S) -> Self::Projected {
238        let (offset, _) = subplace.offset(());
239        // SAFETY: `subplace` guarantees that `offset` produces a valid byte offset
240        // within the allocation of `self`, and references are non-null.
241        unsafe {
242            let new_ptr = (self.ptr.as_ptr() as *mut u8).add(offset) as *mut S::Target;
243            RefHandle { ptr: NonNull::new_unchecked(new_ptr), _lt: PhantomData }
244        }
245    }
246}
247
248/// Handle referring to a place behind an exclusive reference.
249#[unstable(feature = "field_projections", issue = "145383")]
250pub struct MutHandle<'a, T: ?Sized> {
251    ptr: NonNull<T>,
252    _lt: PhantomData<&'a mut T>,
253}
254
255#[unstable(feature = "field_projections", issue = "145383")]
256impl<'a, T: ?Sized> Clone for MutHandle<'a, T> {
257    fn clone(&self) -> Self {
258        *self
259    }
260}
261
262#[unstable(feature = "field_projections", issue = "145383")]
263impl<'a, T: ?Sized> Copy for MutHandle<'a, T> {}
264
265#[unstable(feature = "field_projections", issue = "145383")]
266impl<'a, T: ?Sized> crate::fmt::Debug for MutHandle<'a, T> {
267    fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
268        f.debug_struct("MutHandle").field("ptr", &self.ptr).finish()
269    }
270}
271
272impl<'a, T: ?Sized> MutHandle<'a, T> {
273    /// Obtains a const raw pointer to the place.
274    #[unstable(feature = "field_projections", issue = "145383")]
275    pub const fn as_ptr(&self) -> *const T {
276        self.ptr.as_ptr()
277    }
278
279    /// Obtains a mutable raw pointer to the place.
280    #[unstable(feature = "field_projections", issue = "145383")]
281    pub const fn as_mut_ptr(&self) -> *mut T {
282        self.ptr.as_ptr()
283    }
284
285    /// Obtains a shared reference to the place.
286    #[unstable(feature = "field_projections", issue = "145383")]
287    pub const unsafe fn as_ref(&self) -> &'a T {
288        // SAFETY: Guaranteed by the caller to be a valid dereferenceable reference.
289        unsafe { self.ptr.as_ref() }
290    }
291
292    /// Obtains an exclusive reference to the place.
293    #[unstable(feature = "field_projections", issue = "145383")]
294    pub const unsafe fn as_mut(&mut self) -> &'a mut T {
295        // SAFETY: Guaranteed by the caller to be a valid dereferenceable mutable reference.
296        unsafe { self.ptr.as_mut() }
297    }
298}
299
300#[unstable(feature = "field_projections", issue = "145383")]
301impl<'a, T: ?Sized> PlaceHandle for MutHandle<'a, T> {
302    type Target = T;
303}
304
305#[unstable(feature = "field_projections", issue = "145383")]
306unsafe impl<'a, S> ProjectPlace<S> for MutHandle<'a, S::Source>
307where
308    S: Subplace,
309    S::Source: Sized,
310    S::Target: Sized + 'a,
311{
312    type Projected = MutHandle<'a, S::Target>;
313
314    unsafe fn project_place(self, subplace: S) -> Self::Projected {
315        let (offset, _) = subplace.offset(());
316        // SAFETY: `subplace` guarantees that `offset` produces a valid byte offset
317        // within the allocation of `self`, and references are non-null.
318        unsafe {
319            let new_ptr = (self.ptr.as_ptr() as *mut u8).add(offset) as *mut S::Target;
320            MutHandle { ptr: NonNull::new_unchecked(new_ptr), _lt: PhantomData }
321        }
322    }
323}
324
325#[unstable(feature = "field_projections", issue = "145383")]
326unsafe impl<'a, T: ?Sized> DerefPlace for LocalHandle<&'a mut T> {
327    type PointeeHandle = MutHandle<'a, T>;
328    unsafe fn deref_place(self) -> Self::PointeeHandle {
329        // SAFETY: `self` refers to a valid proxy location containing a mutable pointer.
330        unsafe {
331            let ptr_val = *(self.ptr as *const *mut T);
332            MutHandle { ptr: NonNull::new_unchecked(ptr_val), _lt: PhantomData }
333        }
334    }
335}
336
337#[unstable(feature = "field_projections", issue = "145383")]
338unsafe impl<'a, T: ?Sized> DerefPlace for LocalHandle<&'a T> {
339    type PointeeHandle = RefHandle<'a, T>;
340    unsafe fn deref_place(self) -> Self::PointeeHandle {
341        // SAFETY: `self` refers to a valid proxy location containing a pointer.
342        unsafe {
343            let ptr_val = *(self.ptr as *const *const T);
344            RefHandle { ptr: NonNull::new_unchecked(ptr_val as *mut T), _lt: PhantomData }
345        }
346    }
347}
348
349#[unstable(feature = "field_projections", issue = "145383")]
350unsafe impl<'a, 'b, T: ?Sized> DerefPlace for RefHandle<'b, &'a T> {
351    type PointeeHandle = RefHandle<'a, T>;
352    unsafe fn deref_place(self) -> Self::PointeeHandle {
353        // SAFETY: `self` refers to a valid proxy location containing a reference pointer.
354        unsafe {
355            let ptr_val = *(self.ptr.as_ptr() as *const *const T);
356            RefHandle { ptr: NonNull::new_unchecked(ptr_val as *mut T), _lt: PhantomData }
357        }
358    }
359}
360
361#[unstable(feature = "field_projections", issue = "145383")]
362unsafe impl<'a, 'b, T: ?Sized> DerefPlace for MutHandle<'b, &'a mut T> {
363    type PointeeHandle = MutHandle<'a, T>;
364    unsafe fn deref_place(self) -> Self::PointeeHandle {
365        // SAFETY: `self` refers to a valid proxy location containing a mutable pointer.
366        unsafe {
367            let ptr_val = *(self.ptr.as_ptr() as *const *mut T);
368            MutHandle { ptr: NonNull::new_unchecked(ptr_val), _lt: PhantomData }
369        }
370    }
371}
372
373#[unstable(feature = "field_projections", issue = "145383")]
374unsafe impl<'a, 'b, T: ?Sized> DerefPlace for MutHandle<'b, &'a T> {
375    type PointeeHandle = RefHandle<'a, T>;
376    unsafe fn deref_place(self) -> Self::PointeeHandle {
377        // SAFETY: `self` refers to a valid proxy location containing a reference pointer.
378        unsafe {
379            let ptr_val = *(self.ptr.as_ptr() as *const *const T);
380            RefHandle { ptr: NonNull::new_unchecked(ptr_val as *mut T), _lt: PhantomData }
381        }
382    }
383}
384
385#[unstable(feature = "field_projections", issue = "145383")]
386unsafe impl<T: ?Sized> DerefPlace for LocalHandle<*mut T> {
387    type PointeeHandle = LocalHandle<T>;
388    unsafe fn deref_place(self) -> Self::PointeeHandle {
389        // SAFETY: `self` refers to a valid proxy location containing a pointer.
390        let ptr_val = unsafe { *(self.ptr as *const *mut T) };
391        LocalHandle { ptr: ptr_val }
392    }
393}
394
395#[unstable(feature = "field_projections", issue = "145383")]
396unsafe impl<T: ?Sized> DerefPlace for LocalHandle<*const T> {
397    type PointeeHandle = LocalHandle<T>;
398    unsafe fn deref_place(self) -> Self::PointeeHandle {
399        // SAFETY: `self` refers to a valid proxy location containing a pointer.
400        let ptr_val = unsafe { *(self.ptr as *const *const T) };
401        LocalHandle { ptr: ptr_val as *mut T }
402    }
403}