1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use cratePtr;
/// The bridge that makes `SelfRef` work with any type, sized or unsized.
///
/// Rust has two kinds of types: sized ones like `i32` and `String` that have a known
/// size at compile time, and unsized ones like `[T]`, `str`, and trait objects that
/// need extra metadata to work with. This trait abstracts away that complexity,
/// letting `SelfRef` handle both seamlessly.
///
/// Most users never need to implement this trait directly - it's already implemented
/// for all the types you'd want to use. The magic happens behind the scenes when you
/// create a `SelfRef<[u8]>` or `SelfRef<TraitObject<dyn Debug>>`.
///
/// ```rust
/// use movable_ref::SelfRef;
///
/// // Works with sized types (metadata = ())
/// let mut value = 42i32;
/// let mut ptr: SelfRef<i32> = SelfRef::null();
/// ptr.set(&mut value).unwrap();
///
/// // Also works with slices (metadata = length)
/// let mut data = vec![1, 2, 3, 4, 5];
/// let mut slice_ptr: SelfRef<[i32]> = SelfRef::null();
/// slice_ptr.set(&mut data[1..4]).unwrap(); // Points to middle section
/// ```
///
/// The trait handles the complex pointer arithmetic needed to reconstruct fat pointers
/// from offset-based storage, making `SelfRef` truly universal across Rust's type system.
///
/// # Safety
///
/// Implementations must correctly reconstruct valid pointers. The `compose` method
/// is the critical piece - it takes a raw data pointer and metadata, then builds
/// a proper pointer to `Self`. Get this wrong and you'll have undefined behavior.
pub unsafe