Skip to content

Commit 3bbeb7e

Browse files
committed
port __init_internal! from the kernel
`__init_internal!` combines `try_init!` and `try_pin_init!` to only have a single macro to maintain.
1 parent 0c52973 commit 3bbeb7e

10 files changed

Lines changed: 466 additions & 460 deletions

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ keywords = ["safe", "pin", "init", "no-std", "rust-patterns"]
1414
categories = ["no-std", "rust-patterns", "embedded"]
1515

1616
[dependencies]
17+
paste = "1.0.14"
1718
pinned-init-macro = { path = "./pinned-init-macro", version = "=0.0.4" }
1819

1920
[features]

src/__internal.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ fn stack_init_reuse() {
196196

197197
/// When a value of this type is dropped, it drops a `T`.
198198
///
199-
/// Can be forgotton to prevent the drop.
199+
/// Can be forgotten to prevent the drop.
200200
pub struct DropGuard<T: ?Sized> {
201201
ptr: *mut T,
202-
do_drop: Cell<bool>,
203202
}
204203

205204
impl<T: ?Sized> DropGuard<T> {
@@ -215,32 +214,16 @@ impl<T: ?Sized> DropGuard<T> {
215214
/// - will not be dropped by any other means.
216215
#[inline]
217216
pub unsafe fn new(ptr: *mut T) -> Self {
218-
Self {
219-
ptr,
220-
do_drop: Cell::new(true),
221-
}
222-
}
223-
224-
/// Prevents this guard from dropping the supplied pointer.
225-
///
226-
/// # Safety
227-
///
228-
/// This function is unsafe in order to prevent safe code from forgetting this guard. It should
229-
/// only be called by the macros in this module.
230-
#[inline]
231-
pub unsafe fn forget(&self) {
232-
self.do_drop.set(false);
217+
Self { ptr }
233218
}
234219
}
235220

236221
impl<T: ?Sized> Drop for DropGuard<T> {
237222
#[inline]
238223
fn drop(&mut self) {
239-
if self.do_drop.get() {
240-
// SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
241-
// ensuring that this operation is safe.
242-
unsafe { ptr::drop_in_place(self.ptr) }
243-
}
224+
// SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
225+
// ensuring that this operation is safe.
226+
unsafe { ptr::drop_in_place(self.ptr) }
244227
}
245228
}
246229

0 commit comments

Comments
 (0)