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#[lang = "local_handle"]
97#[unstable(feature = "field_projections", issue = "145383")]
98pub struct LocalHandle<T: ?Sized> {
99 ptr: *mut T,
100}
101
102#[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 #[unstable(feature = "field_projections", issue = "145383")]
130 pub const fn as_ptr(&self) -> *const T {
131 self.ptr
132 }
133
134 #[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 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#[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 #[unstable(feature = "field_projections", issue = "145383")]
211 pub const fn as_ptr(&self) -> *const T {
212 self.ptr.as_ptr()
213 }
214
215 #[unstable(feature = "field_projections", issue = "145383")]
217 pub const unsafe fn as_ref(&self) -> &'a T {
218 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 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#[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 #[unstable(feature = "field_projections", issue = "145383")]
275 pub const fn as_ptr(&self) -> *const T {
276 self.ptr.as_ptr()
277 }
278
279 #[unstable(feature = "field_projections", issue = "145383")]
281 pub const fn as_mut_ptr(&self) -> *mut T {
282 self.ptr.as_ptr()
283 }
284
285 #[unstable(feature = "field_projections", issue = "145383")]
287 pub const unsafe fn as_ref(&self) -> &'a T {
288 unsafe { self.ptr.as_ref() }
290 }
291
292 #[unstable(feature = "field_projections", issue = "145383")]
294 pub const unsafe fn as_mut(&mut self) -> &'a mut T {
295 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 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 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 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 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 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 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 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 let ptr_val = unsafe { *(self.ptr as *const *const T) };
401 LocalHandle { ptr: ptr_val as *mut T }
402 }
403}