Skip to content

Commit c445f53

Browse files
committed
Remove as_raw and from_raw in favor of a documentation update
1 parent 6df4777 commit c445f53

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

text/0000-atomic-access.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
- Feature Name: atomic_access
1+
q- Feature Name: atomic_access
22
- Start Date: 2016-06-15
33
- RFC PR: (leave this empty)
44
- Rust Issue: (leave this empty)
55

66
# Summary
77
[summary]: #summary
88

9-
Add the following methods to atomic types:
9+
This RFC adds the following methods to atomic types:
1010

1111
```rust
1212
impl AtomicT {
1313
fn get_mut(&mut self) -> &mut T;
1414
fn into_inner(self) -> T;
15-
fn as_raw(&self) -> *mut T;
16-
unsafe fn from_raw(ptr: *mut T) -> &AtomicT;
1715
}
1816
```
1917

18+
It also specifies that the layout of an `AtomicT` type is always the same as the underlying `T` type. So, for example, `AtomicI32` is guaranteed to be transmutable to and from `i32`.
19+
2020
# Motivation
2121
[motivation]: #motivation
2222

@@ -28,33 +28,33 @@ A normal load/store is different from a `load(Relaxed)` or `store(Relaxed)` beca
2828

2929
`get_mut` in particular is expected to be useful in `Drop` implementations where you have a `&mut self` and need to read the value of an atomic. `into_inner` somewhat overlaps in functionality with `get_mut`, but it is included to allow extracting the value without requiring the atomic object to be mutable. These methods mirror `Mutex::get_mut` and `Mutex::into_inner`.
3030

31-
## `as_raw` and `from_raw`
31+
## Atomic type layout
3232

33-
These methods are mainly intended to be used for FFI, where a variable of a non-atomic type needs to be modified atomically. The most common example of this is the Linux `futex` system call which takes an `int*` parameter pointing to an integer that is atomically modified by both userspace and the kernel.
33+
The layout guarantee is mainly intended to be used for FFI, where a variable of a non-atomic type needs to be modified atomically. The most common example of this is the Linux `futex` system call which takes an `int*` parameter pointing to an integer that is atomically modified by both userspace and the kernel.
3434

35-
Rust code invoking the `futex` system call so far has simply passed the address of the atomic object directly to the system call. However this makes the assumption that the atomic type has the same layout as the underlying integer type. Using `as_raw` instead makes it clear that the resulting pointer will point to the integer value inside the atomic object.
35+
Rust code invoking the `futex` system call so far has simply passed the address of the atomic object directly to the system call. However this makes the assumption that the atomic type has the same layout as the underlying integer type, which is not currently guaranteed by the documentation.
3636

37-
`from_raw` provides the reverse operation: it allows Rust code to atomically modify a value that was not declared as a atomic type. This is useful when dealing with FFI structs that are shared with a thread managed by a C library. Another example would be to atomically modify a value in a memory mapped file that is shared with another process.
37+
This also allows the reverse operation by casting a pointer: it allows Rust code to atomically modify a value that was not declared as a atomic type. This is useful when dealing with FFI structs that are shared with a thread managed by a C library. Another example would be to atomically modify a value in a memory mapped file that is shared with another process.
3838

3939
# Detailed design
4040
[design]: #detailed-design
4141

42-
The actual implementations of these functions are mostly trivial since they are based on `UnsafeCell::get`. The only exception is `from_raw` which will cast the given pointer to a different type, but that should also be fine.
42+
The actual implementations of these functions are mostly trivial since they are based on `UnsafeCell::get`.
43+
44+
The existing implementations of atomic types already have the same layout as the underlying types (even `AtomicBool` and `bool`), so no change is needed here apart from the documentation.
4345

4446
# Drawbacks
4547
[drawbacks]: #drawbacks
4648

4749
The functionality of `into_inner` somewhat overlaps with `get_mut`.
4850

49-
`from_raw` returns an unbounded lifetime.
51+
We lose the ability to change the layout of atomic types, but this shouldn't be necessary since these types map directly to hardware primitives.
5052

5153
# Alternatives
5254
[alternatives]: #alternatives
5355

5456
The functionality of `get_mut` and `into_inner` can be implemented using `load(Relaxed)`, however the latter can result in worse code because it is poorly handled by the optimizer.
5557

56-
The functionality of `as_raw` and `from_raw` could be achieved using transmutes instead, however this requires making assumptions about the internal layout of the atomic types.
57-
5858
# Unresolved questions
5959
[unresolved]: #unresolved-questions
6060

0 commit comments

Comments
 (0)