# Computing raw pointers to fields

**URL:** <https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155>\
**Category:** Unsafe Code Guidelines\
**Created:** [October 20, 2019, 11:01pm UTC](https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155 "2019-10-20T23:01:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [October 20, 2019, 11:01pm UTC](https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155/1 "2019-10-20T23:01:05Z")

</div>

There have been discussions on raw pointers to fields in the past (e.g., "[[Idea] Pointer to Field](https://internals.rust-lang.org/t/idea-pointer-to-field/10061)" and it's [RFC](https://github.com/rust-lang/rfcs/pull/2708)). This thread is an attempt to collect some of the various discussions and ideas around getting a pointer to an object's field.

Right now, [`&raw const <place>`/`&raw mut <place>` are being worked on](https://github.com/rust-lang/rust/issues/64490) (with a high probability of being stabilized someday, I think). It's a step forward, but it doesn't fully cover raw pointers to fields. Consider, for example:

```rust
struct Struct {
    field: f32,
}

let mut uninit_struct = core::mem::MaybeUninit::<Struct>::uninit();
let struct_ptr = uninit.as_mut_ptr();

```

There are a few ways to get a pointer to the field:

1. Current syntax: `unsafe { &mut (*struct_ptr).field as *mut _ }`.
2. Using the new `&raw` syntax: `unsafe { &raw mut (*struct_ptr).field }`.
3. Using @RustyYato's RFC: `struct_ptr.wrapping_project(Struct.field)`.

Questions:

1. What's the validity status of `unsafe { &raw mut (*struct_ptr).field }`? I believe the validity of dereferencing an uninitialized pointer to obtain a pointer to a field is still undecided. cc @RalfJung
2. Are there other proposals or alternatives?

I'm trying to collect and understand the various ways to obtain a pointer to `struct_ptr`'s `field` (including stable, unstable, and proposed-but-not-accepted).

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [October 20, 2019, 11:39pm UTC](https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155/2 "2019-10-20T23:39:24Z")

</div>

This seems to be the current state of affairs. My RFC will likely be closed/postponed after `&raw mut` hits nightly, and I transfer my [WIP crate](https://github.com/KrishnaSannasi/generic-field-projection) to use it. I think it would be best to try out generic fields as a crate before folding it into std because there is quite a lot of design space (esp. around pin). The Pointer to Field idea was more like an extension to Deref than anything else, making all pointer-like types have a very similar api.

> [@mjbshaw](#):
>
> Using @Yato's RFC: `struct_ptr.wrapping_project(Struct.field)` .

In this case you could use `unsafe { struct_ptr.project_unchecked(Struct.field) }`

> [@mjbshaw](#):
>
> Are there other proposals or alternatives?

There is also this RFC which is sort-of orthogonal, but related

> <https://github.com/rust-lang/rfcs/pull/2666>
>
> Extend match syntax (\`\*\`) and patterns (\`raw const\`, \`raw mut\`) by support for a… limited set of operations for pointers, which involve only address calculation and not actually reading through the pointer value. Make it possible to use these matches to calculate addresses of fields even for \`repr(packed)\` structs and possibly unaligned pointers where an intermediate reference must not be created.
> 
> \[Rendered\](https://github.com/HeroicKatora/rfcs/blob/pointer-match/text/0000-pointer-match.md)
> 
> This should be read as complementary surface syntax to the MIR changes proposed in https://github.com/rust-lang/rfcs/pull/2582 . Now, somewhat coincidentally, the author @RalfJung decided to rewrite the other PR to also include a surface syntax. I was unaware of this parallel process . If desired, one could read the match/pattern syntax as complementary to the place expressions proposed there but since the idea was floating around for a while, I address the downsides in comparison to match in \`Rationale and Alternatives\` already.

> [@mjbshaw](#):
>
> What's the validity status of `unsafe { &raw mut (*struct_ptr).field }` ? I believe the validity of dereferencing an uninitialized pointer to obtain a pointer to a field is still undecided

I am pretty sure that this is meant to be valid based on @RalfJung's comment here

> ... But `&raw const (*base_ptr).field` is allowed, ...

> <https://github.com/rust-lang/rust/issues/64490#issuecomment-544270766>
>
> This is a tracking issue for the RFC "an operator to take a raw reference" (rust…-lang/rfcs#2582), feature(raw\_ref\_op).
> 
> \*\*Steps:\*\*
> 
> \- \[\] Implement the RFC (see \[this comment\](https://github.com/rust-lang/rust/issues/64490#issuecomment-531579111) for a detailed checklist)
> \- \[\] Adjust documentation (\[see instructions on rustc-guide\]\[doc-guide\])
> \- \[\] Stabilization PR (\[see instructions on rustc-guide\]\[stabilization-guide\])
> 
> \[stabilization-guide\]: https://rust-lang.github.io/rustc-guide/stabilization\_guide.html#stabilization-pr
> \[doc-guide\]: https://rust-lang.github.io/rustc-guide/stabilization\_guide.html#documentation-prs
> 
> \*\*Unresolved questions:\*\*
> 
> \- \[\] Maybe the lint should also cover cases that look like \`&\[mut\] \<place\> as \*\[mut|const\] ?T\` in the surface syntax but had a method call inserted, thus manifesting a reference (with the associated guarantees). The lint as described would not fire because the reference actually gets used as such (being passed to \`deref\`). However, what would the lint suggest to do instead? There just is no way to write this code without creating a reference.
> 
> \*\*Implementation history:\*\*
> 
> \* Initial implementation: https://github.com/rust-lang/rust/pull/64588

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [October 21, 2019, 9:15am UTC](https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155/3 "2019-10-21T09:15:14Z")

</div>

> [@mjbshaw](#):
>
> What's the validity status of `unsafe { &raw mut (*struct_ptr).field }` ? I believe the validity of dereferencing an uninitialized pointer to obtain a pointer to a field is still undecided. cc @RalfJung

I think the deref is fine. This is in accordance with [the reference](https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html).

But to be really sure, we should ask the lang team to FCP that given `x: *const !`, doing `&raw const *x` is okay. And more interestingly, so is `x: *const (i32, !)` in `&raw const (*x).0`. To get us started in that direction I [opened a UCG issue](https://github.com/rust-lang/unsafe-code-guidelines/issues/216).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 22, 2024, 5:03pm UTC](https://internals.rust-lang.org/t/computing-raw-pointers-to-fields/11155/4 "2024-12-22T17:03:12Z")

</div>

Closed on December 22, 2024, 5:03pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
