Skip to content

Commit 7b4d181

Browse files
committed
add first batch of feedback from review
1 parent 394cc58 commit 7b4d181

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

library/core/src/mem/drop_guard.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::fmt::{self, Debug};
12
use crate::mem::ManuallyDrop;
23
use crate::ops::{Deref, DerefMut};
34
use crate::ptr;
@@ -26,8 +27,10 @@ use crate::ptr;
2627
/// }
2728
/// ```
2829
#[unstable(feature = "drop_guard", issue = "none")]
29-
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
30-
pub struct DropGuard<T, F>
30+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
31+
#[doc(alias = "ScopeGuard")]
32+
#[doc(alias = "defer")]
33+
pub struct DropGuard<T, F = fn(T)>
3134
where
3235
F: FnOnce(T),
3336
{
@@ -51,7 +54,8 @@ where
5154
/// let guard = DropGuard::new(value, |s| println!("{s}"));
5255
/// ```
5356
#[unstable(feature = "drop_guard", issue = "none")]
54-
pub fn new(inner: T, f: F) -> Self {
57+
#[must_use]
58+
pub const fn new(inner: T, f: F) -> Self {
5559
Self { inner: ManuallyDrop::new(inner), f: ManuallyDrop::new(f) }
5660
}
5761

@@ -62,8 +66,8 @@ where
6266
/// `into_inner` from the `Deref` and `DerefMut` impls.
6367
///
6468
/// It is typically preferred to call this function instead of `mem::forget`
65-
/// because it will appropriately de-initialize the closure stored in the
66-
/// `DropGuard` before returning the stored value.
69+
/// because it will return the stored value and drop variables captured
70+
/// by the closure instead of leaking their owned resources.
6771
///
6872
/// # Example
6973
///
@@ -78,8 +82,6 @@ where
7882
#[unstable(feature = "drop_guard", issue = "none")]
7983
#[inline]
8084
pub fn into_inner(guard: Self) -> T {
81-
// Implementation note: this is the same approach that scopeguard takes.
82-
8385
// First we ensure that dropping the guard will not trigger
8486
// its destructor
8587
let mut guard = ManuallyDrop::new(guard);
@@ -136,3 +138,14 @@ where
136138
f(inner);
137139
}
138140
}
141+
142+
#[unstable(feature = "drop_guard", issue = "none")]
143+
impl<T, F> Debug for DropGuard<T, F>
144+
where
145+
T: Debug,
146+
F: FnOnce(T),
147+
{
148+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149+
fmt::Debug::fmt(&**self, f)
150+
}
151+
}

0 commit comments

Comments
 (0)