|
6 | 6 | [](https://docs.rs/bevy_ptr/latest/bevy_ptr/)
|
7 | 7 | [](https://discord.gg/bevy)
|
8 | 8 |
|
9 |
| -The `bevy_ptr` crate provides low-level abstractions for working with pointers in a more safe way than using rust's raw pointers. |
| 9 | +Pointers in computer programming are objects that store a memory address. They're a fundamental building block for constructing more |
| 10 | +complex data structures. |
10 | 11 |
|
11 |
| -Rust has lifetimed and typed references (`&'a T`), unlifetimed and typed references (`*const T`), but no lifetimed but untyped references. |
12 |
| -`bevy_ptr` adds them, called `Ptr<'a>`, `PtrMut<'a>` and `OwningPtr<'a>`. |
13 |
| -These types are lifetime-checked so can never lead to problems like use-after-frees and must always point to valid data. |
| 12 | +They're also *the* definitive source of memory safety bugs: you can dereference a invalid (null) pointer, access a pointer after the underlying |
| 13 | +memory has been freed, and even ignore type safety and misread or mutate the underlying memory improperly. |
| 14 | + |
| 15 | +Rust is a programming language that heavily relies on its types to enforce correctness, and by proxy, memory safety. As a result, |
| 16 | +Rust has an entire zoo of types for working with pointers, and a graph of safe and unsafe conversions that make working with them safer. |
| 17 | + |
| 18 | +`bevy_ptr` is a crate that attempts to bridge the gap between the full blown unsafety of `*mut ()` and the safe `&'a T`, allowing users |
| 19 | +to choose what invariants to uphold for their pointer, with the intent to enable building progressively safer abstractions. |
| 20 | + |
| 21 | +## How to Build a Borrow (From Scratch) |
| 22 | + |
| 23 | +Correctly and safety converting a pointer into a valid borrow is at the core of all `unsafe` code in Rust. Looking at the documentation for |
| 24 | +[`(*const T)::as_ref`], a pointer must satisfy *all* of the following conditions: |
| 25 | + |
| 26 | +* The pointer must be properly aligned. |
| 27 | +* The pointer cannot be null, even for zero sized types. |
| 28 | +* The pointer must be within bounds of a valid allocated object (on the stack or the heap). |
| 29 | +* The pointer must point to an initialized instance of `T`. |
| 30 | +* The newly assigned lifetime should be valid for the value that the pointer is targeting. |
| 31 | +* The code must enforce Rust's aliasing rules. Only one mutable borrow or arbitrarily many read-only borrows may exist to a value at any given moment |
| 32 | + in time, and converting from `&T` to `&mut T` is never allowed. |
| 33 | + |
| 34 | +Note these rules aren't final and are still in flux as the Rust Project hashes out what exactly are the pointer aliasing rules, but the expectation is that the |
| 35 | +final set of constraints are going to be a superset of this list, not a subset. |
| 36 | + |
| 37 | +This list already is non-trivial to satisfy in isolation. Thankfully, the Rust core/standard library provides a progressive list of pointer types that help |
| 38 | +build these safety guarantees... |
| 39 | + |
| 40 | +## Standard Pointers |
| 41 | + |
| 42 | +|Pointer Type |Lifetime'ed|Mutable|Strongly Typed|Aligned|Not Null|Forbids Aliasing|Forbids Arithmetic| |
| 43 | +|-------------------|-----------|-------|--------------|-------|--------|----------------|------------------| |
| 44 | +|`Box<T>` |Owned |Yes |Yes |Yes |Yes |Yes |Yes | |
| 45 | +|`&'a mut T` |Yes |Yes |Yes |Yes |Yes |Yes |Yes | |
| 46 | +|`&'a T` |Yes |No |Yes |Yes |Yes |No |Yes | |
| 47 | +|`&'a UnsafeCell<T>`|Yes |Maybe |Yes |Yes |Yes |Yes |Yes | |
| 48 | +|`NonNull<T>` |No |Yes |Yes |No |Yes |No |No | |
| 49 | +|`*const T` |No |No |Yes |No |No |No |No | |
| 50 | +|`*mut T` |No |Yes |Yes |No |No |No |No | |
| 51 | +|`*const ()` |No |No |No |No |No |No |No | |
| 52 | +|`*mut ()` |No |Yes |No |No |No |No |No | |
| 53 | + |
| 54 | +`&T`, `&mut T`, and `Box<T>` are by far the most common pointer types that Rust developers will see. They're the only ones in this list that are entirely usable |
| 55 | +without the use of `unsafe`. |
| 56 | + |
| 57 | +`&UnsafeCell<T>` is the first step away from safety. `UnsafeCell` is the *only* way to get a mutable borrow from an immutable one in the language, so it's the |
| 58 | +base primitive for all interior mutability in the language: `Cell<T>`, `RefCell<T>`, `Mutex<T>`, `RwLock<T>`, etc. are all built on top of |
| 59 | +`UnsafeCell<T>`. To safety convert `&UnsafeCell<T>` into a `&T` or `&mut T`, the caller must guarantee that all simultaneous access follow Rust's aliasing rules. |
| 60 | + |
| 61 | +`NonNull<T>` takes quite a step down from the aforementioned types. In addition to allowing aliasing, it's the first pointer type on this list to drop both |
| 62 | +lifetimes and the alignment guarantees of borrows. Its only guarantees are that the pointer is not null and that it points to a valid instance |
| 63 | +of type `T`. If you've ever worked with C++, `NonNull<T>` is very close to a C++ reference (`T&`). |
| 64 | + |
| 65 | +`*const T` and `*mut T` are what most developers with a background in C or C++ would consider pointers. |
| 66 | + |
| 67 | +`*const ()` is the bottom of this list. They're the Rust equivalent to C's `void*`. Note that Rust doesn't formally have a concept of type that holds an arbitrary |
| 68 | +untyped memory address. Pointing at the unit type (or some other zero-sized type) just happens to be the convention. The only way to reasonably use them is to |
| 69 | +cast back to a typed pointer. They show up occasionally when dealing with FFI and the rare occasion where dynamic dispatch is required, but a trait is too |
| 70 | +constraining of an interface to work with. A great example of this are the [RawWaker] APIs, where a singular trait (or set of traits) may be insufficient to capture |
| 71 | +all usage patterns. `*mut ()` should only be used to carry the mutability of the target, and as there is no way to to mutate an unknown type. |
| 72 | + |
| 73 | +[RawWaker]: https://doc.rust-lang.org/std/task/struct.RawWaker.html |
| 74 | + |
| 75 | +## Available in Nightly |
| 76 | + |
| 77 | +|Pointer Type |Lifetime'ed|Mutable|Strongly Typed|Aligned|Not Null|Forbids Aliasing|Forbids Arithmetic| |
| 78 | +|-------------------|-----------|-------|--------------|-------|--------|----------------|------------------| |
| 79 | +|`Unique<T>` |Owned |Yes |Yes |Yes |Yes |Yes |Yes | |
| 80 | +|`Shared<T>` |Owned* |Yes |Yes |Yes |Yes |No |Yes | |
| 81 | + |
| 82 | +`Unique<T>` is currently available in `core::ptr` on nightly Rust builds. It's a pointer type that acts like it owns the value it points to. It can be thought of |
| 83 | +as a `Box<T>` that does not allocate on initialization or deallocated when it's dropped, and is in fact used to implement common types like `Box<T>`, `Vec<T>`, |
| 84 | +etc. |
| 85 | + |
| 86 | +`Shared<T>` is currently available in `core::ptr` on nightly Rust builds. It's the pointer that backs both `Rc<T>` and `Arc<T>`. It's semantics allow for |
| 87 | +multiple instances to collectively own the data it points to, and as a result, forbids getting a mutable borrow. |
| 88 | + |
| 89 | +`bevy_ptr` does not support these types right now, but may support [polyfills] for these pointer types if the need arises. |
| 90 | + |
| 91 | +[polyfills]: https://en.wikipedia.org/wiki/Polyfill_(programming) |
| 92 | + |
| 93 | +## Available in `bevy_ptr` |
| 94 | + |
| 95 | +|Pointer Type |Lifetime'ed|Mutable|Strongly Typed|Aligned|Not Null|Forbids Aliasing|Forbids Arithmetic| |
| 96 | +|---------------------|-----------|-------|--------------|-------|--------|----------------|------------------| |
| 97 | +|`ConstNonNull<T>` |No |No |Yes |No |Yes |No |Yes | |
| 98 | +|`ThinSlicePtr<'a, T>`|Yes |No |Yes |Yes |Yes |Yes |Yes | |
| 99 | +|`OwningPtr<'a>` |Yes |Yes |No |Maybe |Yes |Yes |No | |
| 100 | +|`Ptr<'a>` |Yes |No |No |Maybe |Yes |No |No | |
| 101 | +|`PtrMut<'a>` |Yes |Yes |No |Maybe |Yes |Yes |No | |
| 102 | + |
| 103 | +`ConstNonNull<T>` is like `NonNull<T>` but disallows safe conversions into types that allow mutable access to the value it points to. It's the `*const T` to |
| 104 | +`NonNull<T>`'s `*mut T`. |
| 105 | + |
| 106 | +`ThinSlicePtr<'a, T>` is a `&'a [T]` without the slice length. This means it's smaller on the stack, but it means bounds checking is impossible locally, so |
| 107 | +accessing elements in the slice is `unsafe`. In debug builds, the length is included and will be checked. |
| 108 | + |
| 109 | +`OwningPtr<'a>`, `Ptr<'a>`, and `PtrMut<'a>` act like `NonNull<()>`, but attempts to restore much of the safety guarantees of `Unique<T>`, `&T`, and `&mut T`. |
| 110 | +They allow working with heterogenous type erased storage (i.e. ECS tables, typemaps) without the overhead of dynamic dispatch in a manner that progressively |
| 111 | +translates back to safe borrows. These types also support optional alignment requirements at a type level, and will verify it on dereference in debug builds. |
0 commit comments