update documentation + minor style changes

This commit is contained in:
konsumlamm
2020-02-10 12:21:46 +01:00
parent d8e6009067
commit 5c3b886968
+13 -10
View File
@@ -73,7 +73,7 @@
//!
//! ```
//! extern crate scopeguard;
//!
//!
//! use std::fs::*;
//! use std::io::{self, Write};
//! # // Mock file so that we don't actually write a file
@@ -84,7 +84,7 @@
//! # fn sync_all(&self) -> io::Result<()> { Ok(()) }
//! # }
//! # use self::MockFile as File;
//!
//!
//! fn try_main() -> io::Result<()> {
//! let f = File::create("newfile.txt")?;
//! let mut file = scopeguard::guard(f, |f| {
@@ -114,7 +114,7 @@
//! //
//! // For optimization purposes we temporarily violate an invariant of the
//! // Vec, that it owns all of its elements.
//! //
//! //
//! // The safe approach is to use swap, which means two writes to memory,
//! // the optimization is to use a “hole” which uses only one write of memory
//! // for each position it moves.
@@ -289,7 +289,7 @@ macro_rules! defer_on_unwind {
/// (except if panic or other code aborts; so as long as destructors run).
/// It is run only once.
///
/// The `S` parameter for [`Strategy`](Strategy.t.html) determines if
/// The `S` parameter for [`Strategy`](trait.Strategy.html) determines if
/// the closure actually runs.
///
/// The guard's closure will be called with the held value in the destructor.
@@ -301,6 +301,7 @@ pub struct ScopeGuard<T, F, S = Always>
{
value: ManuallyDrop<T>,
dropfn: ManuallyDrop<F>,
// fn(S) -> S is used, so that the S is not taken into account for auto traits.
strategy: PhantomData<fn(S) -> S>,
}
@@ -325,6 +326,7 @@ impl<T, F, S> ScopeGuard<T, F, S>
///
/// ```
/// extern crate scopeguard;
///
/// use scopeguard::{guard, ScopeGuard};
///
/// fn conditional() -> bool { true }
@@ -344,7 +346,7 @@ impl<T, F, S> ScopeGuard<T, F, S>
/// ```
#[inline]
pub fn into_inner(guard: Self) -> T {
// Cannot pattern match out of Drop-implementing types, so
// Cannot move out of Drop-implementing types, so
// ptr::read the value and forget the guard.
unsafe {
let value = ptr::read(&*guard.value);
@@ -393,14 +395,14 @@ pub fn guard_on_success<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, OnSuccess>
///
/// ```
/// extern crate scopeguard;
///
///
/// use scopeguard::ScopeGuard;
/// # fn main() {
/// {
/// let guard = scopeguard::guard((), |_| { });
/// let guard = scopeguard::guard((), |_| {});
///
/// // rest of the code here
///
///
/// // we reached the end of scope without unwinding - defuse it
/// ScopeGuard::into_inner(guard);
/// }
@@ -421,13 +423,14 @@ unsafe impl<T, F, S> Sync for ScopeGuard<T, F, S>
where T: Sync,
F: FnOnce(T),
S: Strategy
{ }
{}
impl<T, F, S> Deref for ScopeGuard<T, F, S>
where F: FnOnce(T),
S: Strategy
{
type Target = T;
fn deref(&self) -> &T {
&*self.value
}
@@ -438,7 +441,7 @@ impl<T, F, S> DerefMut for ScopeGuard<T, F, S>
S: Strategy
{
fn deref_mut(&mut self) -> &mut T {
&mut*self.value
&mut *self.value
}
}