Skip to content

Commit 9defecc

Browse files
committed
Update to Rust edition 2024
This also addresses #4 since unsafe_op_in_unsafe_fn is now the default.
1 parent 8046922 commit 9defecc

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ authors = ["Michael Lass <bevan@bi-co.net>"]
33
categories = ["data-structures", "memory-management"]
44
description = "Allocate heap memory with user-specified alignment"
55
documentation = "https://docs.rs/aligned_box/"
6-
edition = "2021"
6+
edition = "2024"
77
homepage = "https://github.com/michaellass/aligned_box/"
88
keywords = ["aligned", "alignment", "box", "heap", "memory"]
99
license = "MIT"
@@ -21,4 +21,4 @@ lazy_static = "^1.4"
2121

2222
[features]
2323
default = ["std"]
24-
std = []
24+
std = []

src/lib.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ impl<T: ?Sized> AlignedBox<T> {
128128
/// # Safety
129129
/// The function is unsafe because improper use can lead to issues, such as double-free. Also,
130130
/// behavior is undefined if the given layout does not correspond to the one used for
131-
/// allocation.
131+
/// allocation. Refer to the safety notes of alloc::boxed::Box::from_raw for requirements on
132+
/// ptr.
132133
pub unsafe fn from_raw_parts(ptr: *mut T, layout: alloc::alloc::Layout) -> AlignedBox<T> {
133-
let container = std::mem::ManuallyDrop::new(alloc::boxed::Box::from_raw(ptr));
134+
// SAFETY:
135+
// * Meeting requirements on ptr is the responsibility of the caller.
136+
let container = std::mem::ManuallyDrop::new(unsafe { alloc::boxed::Box::from_raw(ptr) });
134137
AlignedBox::<T> { container, layout }
135138
}
136139
}
@@ -202,7 +205,6 @@ impl<T> AlignedBox<[T]> {
202205
// # SAFETY
203206
// The initializer function has to initialize the value behind the pointer without
204207
// reading or dropping the old uninitialized value, e.g., using std::ptr::write.
205-
#[allow(unused_unsafe)] // https://github.com/rust-lang/rfcs/pull/2585
206208
unsafe fn new_slice(
207209
alignment: usize,
208210
nelems: usize,
@@ -216,10 +218,12 @@ impl<T> AlignedBox<[T]> {
216218

217219
let (ptr, layout) = AlignedBox::<T>::allocate(alignment, nelems)?;
218220

219-
// Initialize values. The caller must ensure that initializer does not expect valid
220-
// values behind ptr.
221+
// Initialize values.
222+
// SAFETY:
223+
// * The caller must ensure that initializer does not expect valid values behind ptr.
224+
// * ptr contains nelemens elements
221225
for i in 0..nelems {
222-
initializer(ptr.add(i));
226+
initializer(unsafe { ptr.add(i) });
223227
}
224228

225229
// SAFETY:
@@ -245,7 +249,6 @@ impl<T> AlignedBox<[T]> {
245249
// # SAFETY
246250
// The initializer function has to initialize the value behind the pointer without
247251
// reading or dropping the old uninitialized value, e.g., using std::ptr::write.
248-
#[allow(unused_unsafe)] // https://github.com/rust-lang/rfcs/pull/2585
249252
unsafe fn realloc(
250253
&mut self,
251254
nelems: usize,
@@ -277,7 +280,9 @@ impl<T> AlignedBox<[T]> {
277280
// * (*ptr)[i] is valid for R/W, properly aligned and valid to drop
278281
// * the element will not be read as it will either be re-initialized using
279282
// std::ptr::write or the slice behind ptr will not be used anymore.
280-
std::ptr::drop_in_place(&mut (*ptr)[i]);
283+
unsafe {
284+
std::ptr::drop_in_place(&mut (*ptr)[i]);
285+
}
281286
}
282287

283288
// SAFETY:
@@ -291,10 +296,14 @@ impl<T> AlignedBox<[T]> {
291296
if new_ptr.is_null() {
292297
// realloc failed. We need to restore a valid state and return an error.
293298

294-
// Reinitialize previously dropped values. The caller must ensure that
295-
// initializer does not expect valid values behind ptr.
299+
// Reinitialize previously dropped values.
300+
// SAFETY:
301+
// * The caller must ensure that initializer does not expect valid values behind ptr.
302+
// * Since realloc failed, ptr still contains old_nelems elements.
296303
for i in nelems..old_nelems {
297-
initializer(&mut (*ptr)[i]);
304+
unsafe {
305+
initializer(&mut (*ptr)[i]);
306+
}
298307
}
299308

300309
// SAFETY:
@@ -304,10 +313,12 @@ impl<T> AlignedBox<[T]> {
304313
return Err(AlignedBoxError::OutOfMemory);
305314
}
306315

307-
// Initialize newly allocated values. The caller must ensure that
308-
// initializer does not expect valid values behind ptr.
316+
// Initialize newly allocated values.
317+
// SAFETY:
318+
// * The caller must ensure that initializer does not expect valid values behind ptr.
319+
// * new_ptr contains nelems elements.
309320
for i in old_nelems..nelems {
310-
initializer(new_ptr.add(i));
321+
initializer(unsafe { new_ptr.add(i) });
311322
}
312323

313324
// Create a new slice, a new Box and update layout.

0 commit comments

Comments
 (0)