ptr_utils/unaligned/
write.rs

1//! Unaligned write operations for pointer types.
2
3/// Trait providing convenient unaligned write operations for mutable pointer types.
4pub trait UnalignedWrite {
5    // Unsigned integer types
6
7    /// Writes a [`u8`] value to the pointer at the given byte offset.
8    ///
9    /// # Safety
10    /// - The pointer plus byte offset must be valid for writing 1 byte
11    /// - The caller must ensure the pointer remains valid for the duration of the write
12    /// - The memory location must be mutable
13    unsafe fn write_u8_at(self, byte_offset: usize, value: u8);
14
15    /// Writes a [`u16`] value to the pointer at the given byte offset.
16    ///
17    /// # Safety
18    /// - The pointer plus byte offset must be valid for writing 2 bytes
19    /// - The caller must ensure the pointer remains valid for the duration of the write
20    /// - The memory location must be mutable
21    /// - No alignment requirements - this performs unaligned writes
22    unsafe fn write_u16_at(self, byte_offset: usize, value: u16);
23
24    /// Writes a [`u32`] value to the pointer at the given byte offset.
25    ///
26    /// # Safety
27    /// - The pointer plus byte offset must be valid for writing 4 bytes
28    /// - The caller must ensure the pointer remains valid for the duration of the write
29    /// - The memory location must be mutable
30    /// - No alignment requirements - this performs unaligned writes
31    unsafe fn write_u32_at(self, byte_offset: usize, value: u32);
32
33    /// Writes a [`u64`] value to the pointer at the given byte offset.
34    ///
35    /// # Safety
36    /// - The pointer plus byte offset must be valid for writing 8 bytes
37    /// - The caller must ensure the pointer remains valid for the duration of the write
38    /// - The memory location must be mutable
39    /// - No alignment requirements - this performs unaligned writes
40    unsafe fn write_u64_at(self, byte_offset: usize, value: u64);
41
42    /// Writes a [`u128`] value to the pointer at the given byte offset.
43    ///
44    /// # Safety
45    /// - The pointer plus byte offset must be valid for writing 16 bytes
46    /// - The caller must ensure the pointer remains valid for the duration of the write
47    /// - The memory location must be mutable
48    /// - No alignment requirements - this performs unaligned writes
49    unsafe fn write_u128_at(self, byte_offset: usize, value: u128);
50
51    /// Writes a [`usize`] value to the pointer at the given byte offset.
52    ///
53    /// # Safety
54    /// - The pointer plus byte offset must be valid for writing [`size_of::<usize>()`] bytes
55    /// - The caller must ensure the pointer remains valid for the duration of the write
56    /// - The memory location must be mutable
57    /// - No alignment requirements - this performs unaligned writes
58    unsafe fn write_usize_at(self, byte_offset: usize, value: usize);
59
60    // Signed integer types
61
62    /// Writes an [`i8`] value to the pointer at the given byte offset.
63    ///
64    /// # Safety
65    /// - The pointer plus byte offset must be valid for writing 1 byte
66    /// - The caller must ensure the pointer remains valid for the duration of the write
67    /// - The memory location must be mutable
68    unsafe fn write_i8_at(self, byte_offset: usize, value: i8);
69
70    /// Writes an [`i16`] value to the pointer at the given byte offset.
71    ///
72    /// # Safety
73    /// - The pointer plus byte offset must be valid for writing 2 bytes
74    /// - The caller must ensure the pointer remains valid for the duration of the write
75    /// - The memory location must be mutable
76    /// - No alignment requirements - this performs unaligned writes
77    unsafe fn write_i16_at(self, byte_offset: usize, value: i16);
78
79    /// Writes an [`i32`] value to the pointer at the given byte offset.
80    ///
81    /// # Safety
82    /// - The pointer plus byte offset must be valid for writing 4 bytes
83    /// - The caller must ensure the pointer remains valid for the duration of the write
84    /// - The memory location must be mutable
85    /// - No alignment requirements - this performs unaligned writes
86    unsafe fn write_i32_at(self, byte_offset: usize, value: i32);
87
88    /// Writes an [`i64`] value to the pointer at the given byte offset.
89    ///
90    /// # Safety
91    /// - The pointer plus byte offset must be valid for writing 8 bytes
92    /// - The caller must ensure the pointer remains valid for the duration of the write
93    /// - The memory location must be mutable
94    /// - No alignment requirements - this performs unaligned writes
95    unsafe fn write_i64_at(self, byte_offset: usize, value: i64);
96
97    /// Writes an [`i128`] value to the pointer at the given byte offset.
98    ///
99    /// # Safety
100    /// - The pointer plus byte offset must be valid for writing 16 bytes
101    /// - The caller must ensure the pointer remains valid for the duration of the write
102    /// - The memory location must be mutable
103    /// - No alignment requirements - this performs unaligned writes
104    unsafe fn write_i128_at(self, byte_offset: usize, value: i128);
105
106    /// Writes an [`isize`] value to the pointer at the given byte offset.
107    ///
108    /// # Safety
109    /// - The pointer plus byte offset must be valid for writing [`size_of::<isize>()`] bytes
110    /// - The caller must ensure the pointer remains valid for the duration of the write
111    /// - The memory location must be mutable
112    /// - No alignment requirements - this performs unaligned writes
113    unsafe fn write_isize_at(self, byte_offset: usize, value: isize);
114
115    // Floating point types
116
117    /// Writes an [`f32`] value to the pointer at the given byte offset.
118    ///
119    /// # Safety
120    /// - The pointer plus byte offset must be valid for writing 4 bytes
121    /// - The caller must ensure the pointer remains valid for the duration of the write
122    /// - The memory location must be mutable
123    /// - No alignment requirements - this performs unaligned writes
124    unsafe fn write_f32_at(self, byte_offset: usize, value: f32);
125
126    /// Writes an [`f64`] value to the pointer at the given byte offset.
127    ///
128    /// # Safety
129    /// - The pointer plus byte offset must be valid for writing 8 bytes
130    /// - The caller must ensure the pointer remains valid for the duration of the write
131    /// - The memory location must be mutable
132    /// - No alignment requirements - this performs unaligned writes
133    unsafe fn write_f64_at(self, byte_offset: usize, value: f64);
134
135    // Boolean type
136
137    /// Writes a [`bool`] value to the pointer at the given byte offset.
138    ///
139    /// # Safety
140    /// - The pointer plus byte offset must be valid for writing 1 byte
141    /// - The caller must ensure the pointer remains valid for the duration of the write
142    /// - The memory location must be mutable
143    unsafe fn write_bool_at(self, byte_offset: usize, value: bool);
144}
145
146impl<T> UnalignedWrite for *mut T {
147    #[inline(always)]
148    unsafe fn write_u8_at(self, byte_offset: usize, value: u8) {
149        (self as *mut u8).add(byte_offset).write_unaligned(value);
150    }
151
152    #[inline(always)]
153    unsafe fn write_u16_at(self, byte_offset: usize, value: u16) {
154        ((self as *mut u8).add(byte_offset) as *mut u16).write_unaligned(value);
155    }
156
157    #[inline(always)]
158    unsafe fn write_u32_at(self, byte_offset: usize, value: u32) {
159        ((self as *mut u8).add(byte_offset) as *mut u32).write_unaligned(value);
160    }
161
162    #[inline(always)]
163    unsafe fn write_u64_at(self, byte_offset: usize, value: u64) {
164        ((self as *mut u8).add(byte_offset) as *mut u64).write_unaligned(value);
165    }
166
167    #[inline(always)]
168    unsafe fn write_u128_at(self, byte_offset: usize, value: u128) {
169        ((self as *mut u8).add(byte_offset) as *mut u128).write_unaligned(value);
170    }
171
172    #[inline(always)]
173    unsafe fn write_usize_at(self, byte_offset: usize, value: usize) {
174        ((self as *mut u8).add(byte_offset) as *mut usize).write_unaligned(value);
175    }
176
177    #[inline(always)]
178    unsafe fn write_i8_at(self, byte_offset: usize, value: i8) {
179        ((self as *mut u8).add(byte_offset) as *mut i8).write_unaligned(value);
180    }
181
182    #[inline(always)]
183    unsafe fn write_i16_at(self, byte_offset: usize, value: i16) {
184        ((self as *mut u8).add(byte_offset) as *mut i16).write_unaligned(value);
185    }
186
187    #[inline(always)]
188    unsafe fn write_i32_at(self, byte_offset: usize, value: i32) {
189        ((self as *mut u8).add(byte_offset) as *mut i32).write_unaligned(value);
190    }
191
192    #[inline(always)]
193    unsafe fn write_i64_at(self, byte_offset: usize, value: i64) {
194        ((self as *mut u8).add(byte_offset) as *mut i64).write_unaligned(value);
195    }
196
197    #[inline(always)]
198    unsafe fn write_i128_at(self, byte_offset: usize, value: i128) {
199        ((self as *mut u8).add(byte_offset) as *mut i128).write_unaligned(value);
200    }
201
202    #[inline(always)]
203    unsafe fn write_isize_at(self, byte_offset: usize, value: isize) {
204        ((self as *mut u8).add(byte_offset) as *mut isize).write_unaligned(value);
205    }
206
207    #[inline(always)]
208    unsafe fn write_f32_at(self, byte_offset: usize, value: f32) {
209        ((self as *mut u8).add(byte_offset) as *mut f32).write_unaligned(value);
210    }
211
212    #[inline(always)]
213    unsafe fn write_f64_at(self, byte_offset: usize, value: f64) {
214        ((self as *mut u8).add(byte_offset) as *mut f64).write_unaligned(value);
215    }
216
217    #[inline(always)]
218    unsafe fn write_bool_at(self, byte_offset: usize, value: bool) {
219        ((self as *mut u8).add(byte_offset) as *mut bool).write_unaligned(value);
220    }
221}