aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2024-02-07 21:18:58 +0100
committerJeff Vander Stoep <jeffv@google.com>2024-02-07 21:18:59 +0100
commit7289ba40bb41d88083d60519d70586696677579b (patch)
tree5d36c4198f67562c7c61c96f1a4da7cb52e71d9a
parent5097bb25949d90053b9be02dbc787a286308c08e (diff)
downloadscopeguard-emu-34-3-release.tar.gz
Upgrade scopeguard to 1.2.0emu-34-3-release
This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update external/rust/crates/scopeguard For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md Test: TreeHugger Change-Id: I062c122d9b8fb03195d8ea3766cb098dea40d1cd
-rw-r--r--.cargo_vcs_info.json7
-rw-r--r--.github/workflows/ci.yaml49
-rw-r--r--.travis.yml20
-rw-r--r--Android.bp4
-rw-r--r--Cargo.toml35
-rw-r--r--Cargo.toml.orig4
-rw-r--r--METADATA25
-rw-r--r--README.md102
-rw-r--r--README.rst103
-rw-r--r--examples/readme.rs10
-rw-r--r--src/lib.rs97
11 files changed, 260 insertions, 196 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 55e0c60..f0a14e9 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,6 @@
{
"git": {
- "sha1": "c1ae9969f7c9bc9013602b4bf29bb789e6e7eb16"
- }
-}
+ "sha1": "bb988222848d39b0b9037b29da3a3a1eb05ebf4b"
+ },
+ "path_in_vcs": ""
+} \ No newline at end of file
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
new file mode 100644
index 0000000..9eef849
--- /dev/null
+++ b/.github/workflows/ci.yaml
@@ -0,0 +1,49 @@
+name: CI
+
+on:
+ - push
+ - pull_request
+
+env:
+ CARGO_TERM_COLOR: always
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ rust:
+ - stable
+ - beta
+ - nightly
+ - 1.20.0 # MSRV
+ steps:
+ - uses: actions/checkout@v2
+ - name: Install Rust
+ uses: actions-rs/toolchain@v1
+ with:
+ profile: minimal
+ toolchain: ${{ matrix.rust }}
+ override: true
+ - name: Build
+ run: cargo build
+ - name: Run tests
+ run: cargo test
+ - name: Run tests (without default features)
+ run: cargo test --no-default-features
+
+ no_std:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - name: Test no_std support
+ run: |
+ rustup target add thumbv6m-none-eabi
+ cargo build --no-default-features --target thumbv6m-none-eabi
+
+ format:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - name: Check formatting
+ run: cargo fmt -- --check
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index a803d58..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,20 +0,0 @@
-language: rust
-sudo: false
-
-rust:
- - 1.20.0
- - stable
- - beta
- - nightly
-
-branches:
- only:
- - master
-
-# the main build
-script:
- - |
- cargo build --no-default-features &&
- cargo test --no-default-features &&
- cargo build &&
- cargo test
diff --git a/Android.bp b/Android.bp
index 3d212ac..5fc64ed 100644
--- a/Android.bp
+++ b/Android.bp
@@ -42,7 +42,7 @@ rust_library {
host_supported: true,
crate_name: "scopeguard",
cargo_env_compat: true,
- cargo_pkg_version: "1.1.0",
+ cargo_pkg_version: "1.2.0",
srcs: ["src/lib.rs"],
edition: "2015",
features: [
@@ -63,7 +63,7 @@ rust_test {
host_supported: true,
crate_name: "scopeguard",
cargo_env_compat: true,
- cargo_pkg_version: "1.1.0",
+ cargo_pkg_version: "1.2.0",
srcs: ["src/lib.rs"],
test_suites: ["general-tests"],
auto_gen_config: true,
diff --git a/Cargo.toml b/Cargo.toml
index 9a5b836..acf5dce 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,23 +3,38 @@
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies
+# to registry (e.g., crates.io) dependencies.
#
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
[package]
name = "scopeguard"
-version = "1.1.0"
+version = "1.2.0"
authors = ["bluss"]
-description = "A RAII scope guard that will run a given closure when it goes out of scope,\neven if the code between panics (assuming unwinding panic).\n\nDefines the macros `defer!`, `defer_on_unwind!`, `defer_on_success!` as\nshorthands for guards with one of the implemented strategies.\n"
+description = """
+A RAII scope guard that will run a given closure when it goes out of scope,
+even if the code between panics (assuming unwinding panic).
+
+Defines the macros `defer!`, `defer_on_unwind!`, `defer_on_success!` as
+shorthands for guards with one of the implemented strategies.
+"""
documentation = "https://docs.rs/scopeguard/"
-keywords = ["scope-guard", "defer", "panic", "unwind"]
-categories = ["rust-patterns", "no-std"]
-license = "MIT/Apache-2.0"
+readme = "README.md"
+keywords = [
+ "scope-guard",
+ "defer",
+ "panic",
+ "unwind",
+]
+categories = [
+ "rust-patterns",
+ "no-std",
+]
+license = "MIT OR Apache-2.0"
repository = "https://github.com/bluss/scopeguard"
+
[package.metadata.release]
no-dev-version = true
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 0c727ce..04c8e84 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,8 +1,8 @@
[package]
name = "scopeguard"
-version = "1.1.0"
+version = "1.2.0"
-license = "MIT/Apache-2.0"
+license = "MIT OR Apache-2.0"
repository = "https://github.com/bluss/scopeguard"
documentation = "https://docs.rs/scopeguard/"
authors = ["bluss"]
diff --git a/METADATA b/METADATA
index d050d8c..63329c7 100644
--- a/METADATA
+++ b/METADATA
@@ -1,19 +1,20 @@
+# This project was upgraded with external_updater.
+# Usage: tools/external_updater/updater.sh update external/rust/crates/scopeguard
+# For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md
+
name: "scopeguard"
description: "A RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic). Defines the macros `defer!`, `defer_on_unwind!`, `defer_on_success!` as shorthands for guards with one of the implemented strategies."
third_party {
- url {
- type: HOMEPAGE
- value: "https://crates.io/crates/scopeguard"
- }
- url {
- type: ARCHIVE
- value: "https://static.crates.io/crates/scopeguard/scopeguard-1.1.0.crate"
- }
- version: "1.1.0"
license_type: NOTICE
last_upgrade_date {
- year: 2020
- month: 10
- day: 14
+ year: 2024
+ month: 2
+ day: 7
+ }
+ homepage: "https://crates.io/crates/scopeguard"
+ identifier {
+ type: "Archive"
+ value: "https://static.crates.io/crates/scopeguard/scopeguard-1.2.0.crate"
+ version: "1.2.0"
}
}
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..01fa68c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,102 @@
+# scopeguard
+
+Rust crate for a convenient RAII scope guard that will run a given closure when
+it goes out of scope, even if the code between panics (assuming unwinding panic).
+
+The `defer!` macro and `guard` are `no_std` compatible (require only `core`),
+but the on unwinding / not on unwinding strategies require linking to `std`.
+By default, the `use_std` crate feature is enabled. Disable the default features
+for `no_std` support.
+
+Please read the [API documentation here](https://docs.rs/scopeguard/).
+
+Minimum supported Rust version: 1.20
+
+[![build_status](https://github.com/bluss/scopeguard/actions/workflows/ci.yaml/badge.svg)](https://github.com/bluss/scopeguard/actions/workflows/ci.yaml)
+[![crates](https://img.shields.io/crates/v/scopeguard.svg)](https://crates.io/crates/scopeguard)
+
+## How to use
+
+```rs
+#[macro_use(defer)]
+extern crate scopeguard;
+
+use scopeguard::guard;
+
+fn f() {
+ defer! {
+ println!("Called at return or panic");
+ }
+ panic!();
+}
+
+use std::fs::File;
+use std::io::Write;
+
+fn g() {
+ let f = File::create("newfile.txt").unwrap();
+ let mut file = guard(f, |f| {
+ // write file at return or panic
+ let _ = f.sync_all();
+ });
+ // access the file through the scope guard itself
+ file.write_all(b"test me\n").unwrap();
+}
+```
+
+## Recent Changes
+
+- 1.2.0
+
+ - Use ManuallyDrop instead of mem::forget in into_inner. (by @willtunnels)
+ - Warn if the guard is not assigned to a variable and is dropped immediately
+ instead of at the scope's end. (by @sergey-v-galtsev)
+
+- 1.1.0
+
+ - Change macros (`defer!`, `defer_on_success!` and `defer_on_unwind!`)
+ to accept statements. (by @konsumlamm)
+
+- 1.0.0
+
+ - Change the closure type from `FnMut(&mut T)` to `FnOnce(T)`:
+ Passing the inner value by value instead of a mutable reference is a
+ breaking change, but allows the guard closure to consume it. (by @tormol)
+
+ - Add `defer_on_success!`, `guard_on_success()` and `OnSuccess`
+ strategy, which triggers when scope is exited *without* panic. It's the
+ opposite to `defer_on_unwind!` / `guard_on_unwind()` / `OnUnwind`.
+
+ - Add `ScopeGuard::into_inner()`, which "defuses" the guard and returns the
+ guarded value. (by @tormol)
+
+ - Implement `Sync` for guards with non-`Sync` closures.
+
+ - Require Rust 1.20
+
+- 0.3.3
+
+ - Use `#[inline]` on a few more functions by @stjepang (#14)
+ - Add examples to crate documentation
+
+- 0.3.2
+
+ - Add crate categories
+
+- 0.3.1
+
+ - Add `defer_on_unwind!`, `Strategy` trait
+ - Rename `Guard` → `ScopeGuard`
+ - Add `ScopeGuard::with_strategy`.
+ - `ScopeGuard` now implements `Debug`.
+ - Require Rust 1.11
+
+- 0.2.0
+
+ - Require Rust 1.6
+ - Use `no_std` unconditionally
+ - No other changes
+
+- 0.1.2
+
+ - Add macro `defer!`
diff --git a/README.rst b/README.rst
deleted file mode 100644
index 019ffe7..0000000
--- a/README.rst
+++ /dev/null
@@ -1,103 +0,0 @@
-
-scopeguard
-==========
-
-Rust crate for a convenient RAII scope guard that will run a given closure when
-it goes out of scope, even if the code between panics (assuming unwinding panic).
-
-The `defer!` macro and `guard` are `no_std` compatible (require only core),
-but the on unwinding / not on uwinding strategies requires linking to `std`.
-
-Requires Rust 1.20.
-
-
-Please read the `API documentation here`__
-
-__ https://docs.rs/scopeguard/
-
-|build_status|_ |crates|_
-
-.. |build_status| image:: https://travis-ci.org/bluss/scopeguard.svg
-.. _build_status: https://travis-ci.org/bluss/scopeguard
-
-.. |crates| image:: http://meritbadge.herokuapp.com/scopeguard
-.. _crates: https://crates.io/crates/scopeguard
-
-How to use
-----------
-
-.. code:: rust
-
- #[macro_use(defer)] extern crate scopeguard;
-
- use scopeguard::guard;
-
- fn f() {
- defer!(println!("Called at return or panic"));
- panic!();
- }
-
- use std::fs::File;
- use std::io::Write;
-
- fn g() {
- let f = File::create("newfile.txt").unwrap();
- let mut file = guard(f, |f| {
- // write file at return or panic
- let _ = f.sync_all();
- });
- // Access the file through the scope guard itself
- file.write_all(b"test me\n").unwrap();
- }
-
-Recent Changes
---------------
-
-- 1.1.0
-
- - Change macros (``defer!``, ``defer_on_success!`` and ``defer_on_unwind!``)
- to accept statements. (by @konsumlamm)
-
-- 1.0.0
-
- - Change the closure type from ``FnMut(&mut T)`` to ``FnOnce(T)``:
- Passing the inner value by value instead of a mutable reference is a
- breaking change, but allows the guard closure to consume it. (by @tormol)
-
- - Add ``defer_on_success!{}``, ``guard_on_success()`` and ``OnSuccess``
- strategy, which triggers when scope is exited *without* panic. It's the
- opposite to ``OnUnwind`` / ``guard_on_unwind()`` / ``defer_on_unwind!{}``.
-
- - Add ``ScopeGuard::into_inner()``, which "defuses" the guard and returns the
- guarded value. (by @tormol)
-
- - Implement ``Sync`` for guards with non-``Sync`` closures.
-
- - Require Rust 1.20
-
-- 0.3.3
-
- - Use ``#[inline]`` on a few more functions by @stjepang (#14)
- - Add examples to crate documentation
-
-- 0.3.2
-
- - Add crate categories
-
-- 0.3.1
-
- - Add ``defer_on_unwind!``, ``Strategy`` trait
- - Rename ``Guard`` → ``ScopeGuard``
- - Add ``ScopeGuard::with_strategy``.
- - ``ScopeGuard`` now implements ``Debug``.
- - Require Rust 1.11
-
-- 0.2.0
-
- - Require Rust 1.6
- - Use `no_std` unconditionally
- - No other changes
-
-- 0.1.2
-
- - Add macro ``defer!()``
diff --git a/examples/readme.rs b/examples/readme.rs
index 0710f9f..c4567e5 100644
--- a/examples/readme.rs
+++ b/examples/readme.rs
@@ -1,10 +1,12 @@
-
-#[macro_use(defer)] extern crate scopeguard;
+#[macro_use(defer)]
+extern crate scopeguard;
use scopeguard::guard;
fn f() {
- defer!(println!("Called at return or panic"));
+ defer! {
+ println!("Called at return or panic");
+ }
panic!();
}
@@ -17,7 +19,7 @@ fn g() {
// write file at return or panic
let _ = f.sync_all();
});
- // Access the file through the scope guard itself
+ // access the file through the scope guard itself
file.write_all(b"test me\n").unwrap();
}
diff --git a/src/lib.rs b/src/lib.rs
index 39f5026..d94129f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -153,7 +153,7 @@
//! if *hole_guard.value >= hole_guard.v[i] {
//! // move the element back and the hole forward
//! let index = hole_guard.index;
-//! ptr::copy_nonoverlapping(&hole_guard.v[index + 1], &mut hole_guard.v[index], 1);
+//! hole_guard.v.swap(index, index + 1);
//! hole_guard.index += 1;
//! } else {
//! break;
@@ -193,7 +193,7 @@ extern crate core as std;
use std::fmt;
use std::marker::PhantomData;
-use std::mem::{self, ManuallyDrop};
+use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::ptr;
@@ -228,19 +228,25 @@ pub enum OnSuccess {}
impl Strategy for Always {
#[inline(always)]
- fn should_run() -> bool { true }
+ fn should_run() -> bool {
+ true
+ }
}
#[cfg(feature = "use_std")]
impl Strategy for OnUnwind {
#[inline]
- fn should_run() -> bool { std::thread::panicking() }
+ fn should_run() -> bool {
+ std::thread::panicking()
+ }
}
#[cfg(feature = "use_std")]
impl Strategy for OnSuccess {
#[inline]
- fn should_run() -> bool { !std::thread::panicking() }
+ fn should_run() -> bool {
+ !std::thread::panicking()
+ }
}
/// Macro to create a `ScopeGuard` (always run).
@@ -296,8 +302,9 @@ macro_rules! defer_on_unwind {
///
/// The `ScopeGuard` implements `Deref` so that you can access the inner value.
pub struct ScopeGuard<T, F, S = Always>
- where F: FnOnce(T),
- S: Strategy,
+where
+ F: FnOnce(T),
+ S: Strategy,
{
value: ManuallyDrop<T>,
dropfn: ManuallyDrop<F>,
@@ -306,14 +313,16 @@ pub struct ScopeGuard<T, F, S = Always>
}
impl<T, F, S> ScopeGuard<T, F, S>
- where F: FnOnce(T),
- S: Strategy,
+where
+ F: FnOnce(T),
+ S: Strategy,
{
/// Create a `ScopeGuard` that owns `v` (accessible through deref) and calls
/// `dropfn` when its destructor runs.
///
/// The `Strategy` decides whether the scope guard's closure should run.
#[inline]
+ #[must_use]
pub fn with_strategy(v: T, dropfn: F) -> ScopeGuard<T, F, S> {
ScopeGuard {
value: ManuallyDrop::new(v),
@@ -334,7 +343,7 @@ impl<T, F, S> ScopeGuard<T, F, S>
/// fn main() {
/// let mut guard = guard(Vec::new(), |mut v| v.clear());
/// guard.push(1);
- ///
+ ///
/// if conditional() {
/// // a condition maybe makes us decide to
/// // “defuse” the guard and get back its inner parts
@@ -346,26 +355,26 @@ impl<T, F, S> ScopeGuard<T, F, S>
/// ```
#[inline]
pub fn into_inner(guard: Self) -> T {
- // Cannot move out of Drop-implementing types, so
- // ptr::read the value and forget the guard.
+ // Cannot move out of `Drop`-implementing types,
+ // so `ptr::read` the value and forget the guard.
+ let mut guard = ManuallyDrop::new(guard);
unsafe {
let value = ptr::read(&*guard.value);
- // read the closure so that it is dropped, and assign it to a local
- // variable to ensure that it is only dropped after the guard has
- // been forgotten. (In case the Drop impl of the closure, or that
- // of any consumed captured variable, panics).
- let _dropfn = ptr::read(&*guard.dropfn);
- mem::forget(guard);
+ // Drop the closure after `value` has been read, so that if the
+ // closure's `drop` function panics, unwinding still tries to drop
+ // `value`.
+ ManuallyDrop::drop(&mut guard.dropfn);
value
}
}
}
-
/// Create a new `ScopeGuard` owning `v` and with deferred closure `dropfn`.
#[inline]
+#[must_use]
pub fn guard<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, Always>
- where F: FnOnce(T)
+where
+ F: FnOnce(T),
{
ScopeGuard::with_strategy(v, dropfn)
}
@@ -375,8 +384,10 @@ pub fn guard<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, Always>
/// Requires crate feature `use_std`.
#[cfg(feature = "use_std")]
#[inline]
+#[must_use]
pub fn guard_on_success<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, OnSuccess>
- where F: FnOnce(T)
+where
+ F: FnOnce(T),
{
ScopeGuard::with_strategy(v, dropfn)
}
@@ -410,8 +421,10 @@ pub fn guard_on_success<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, OnSuccess>
/// ```
#[cfg(feature = "use_std")]
#[inline]
+#[must_use]
pub fn guard_on_unwind<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, OnUnwind>
- where F: FnOnce(T)
+where
+ F: FnOnce(T),
{
ScopeGuard::with_strategy(v, dropfn)
}
@@ -420,14 +433,17 @@ pub fn guard_on_unwind<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, OnUnwind>
// not accessible from references.
// The guard does not store any instance of S, so it is also irrelevant.
unsafe impl<T, F, S> Sync for ScopeGuard<T, F, S>
- where T: Sync,
- F: FnOnce(T),
- S: Strategy
-{}
+where
+ T: Sync,
+ F: FnOnce(T),
+ S: Strategy,
+{
+}
impl<T, F, S> Deref for ScopeGuard<T, F, S>
- where F: FnOnce(T),
- S: Strategy
+where
+ F: FnOnce(T),
+ S: Strategy,
{
type Target = T;
@@ -437,8 +453,9 @@ impl<T, F, S> Deref for ScopeGuard<T, F, S>
}
impl<T, F, S> DerefMut for ScopeGuard<T, F, S>
- where F: FnOnce(T),
- S: Strategy
+where
+ F: FnOnce(T),
+ S: Strategy,
{
fn deref_mut(&mut self) -> &mut T {
&mut *self.value
@@ -446,15 +463,14 @@ impl<T, F, S> DerefMut for ScopeGuard<T, F, S>
}
impl<T, F, S> Drop for ScopeGuard<T, F, S>
- where F: FnOnce(T),
- S: Strategy
+where
+ F: FnOnce(T),
+ S: Strategy,
{
fn drop(&mut self) {
// This is OK because the fields are `ManuallyDrop`s
// which will not be dropped by the compiler.
- let (value, dropfn) = unsafe {
- (ptr::read(&*self.value), ptr::read(&*self.dropfn))
- };
+ let (value, dropfn) = unsafe { (ptr::read(&*self.value), ptr::read(&*self.dropfn)) };
if S::should_run() {
dropfn(value);
}
@@ -462,14 +478,15 @@ impl<T, F, S> Drop for ScopeGuard<T, F, S>
}
impl<T, F, S> fmt::Debug for ScopeGuard<T, F, S>
- where T: fmt::Debug,
- F: FnOnce(T),
- S: Strategy
+where
+ T: fmt::Debug,
+ F: FnOnce(T),
+ S: Strategy,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct(stringify!(ScopeGuard))
- .field("value", &*self.value)
- .finish()
+ .field("value", &*self.value)
+ .finish()
}
}