|
2 | 2 |
|
3 | 3 | [](https://github.com/rust-osdev/volatile/actions?query=workflow%3ABuild) [](https://docs.rs/volatile/)
|
4 | 4 |
|
5 |
| -Provides the wrapper type `Volatile`, which wraps a reference to any copy-able type and allows for volatile memory access to wrapped value. Volatile memory accesses are never optimized away by the compiler, and are useful in many low-level systems programming and concurrent contexts. |
| 5 | +Provides volatile wrapper types for raw pointers. |
6 | 6 |
|
7 |
| -The wrapper types *do not* enforce any atomicity guarantees; to also get atomicity, consider looking at the `Atomic` wrapper types found in `libcore` or `libstd`. |
| 7 | +The volatile wrapper types in this crate wrap a pointer to any [`Copy`]-able type and provide volatile memory access to wrapped value. |
| 8 | +Volatile memory accesses are never optimized away by the compiler, and are useful in many low-level systems programming and concurrent contexts. |
| 9 | + |
| 10 | +This crate provides two different wrapper types: [`VolatilePtr`] and [`VolatileRef`]. |
| 11 | +The difference between the two types is that the former behaves like a raw pointer, while the latter behaves like a Rust reference type. |
| 12 | +For example, `VolatilePtr` can be freely copied, but not sent across threads because this could introduce mutable aliasing. |
| 13 | +The `VolatileRef` type, on the other hand, requires exclusive access for mutation, so that sharing it across thread boundaries is safe. |
| 14 | + |
| 15 | +Both wrapper types *do not* enforce any atomicity guarantees; to also get atomicity, consider looking at the `Atomic` wrapper types found in `libcore` or `libstd`. |
| 16 | + |
| 17 | +## Why is there no `VolatileCell`? |
| 18 | + |
| 19 | +Many people expressed interest in a `VolatileCell` type, i.e. a transparent wrapper type that owns the wrapped value. |
| 20 | +Such a type would be similar to [`core::cell::Cell`], with the difference that all methods are volatile. |
| 21 | +Unfortunately, it is not sound to implement such a `VolatileCell` type in Rust. |
| 22 | +The reason is that Rust and LLVM consider `&` and `&mut` references as _dereferencable_. |
| 23 | +This means that the compiler is allowed to freely access the referenced value without any restrictions. |
| 24 | +So no matter how a `VolatileCell` type is implemented, the compiler is allowed to perform non-volatile read operations of the contained value, which can lead to unexpected (or even undefined?) behavior. |
| 25 | +For more details, see the discussion [in our repository](https://github.com/rust-osdev/volatile/issues/31) and [in the `unsafe-code-guidelines` repository](https://github.com/rust-lang/unsafe-code-guidelines/issues/411). |
8 | 26 |
|
9 | 27 | ## License
|
10 | 28 |
|
|
0 commit comments