Skip to content

Commit c3730ae

Browse files
authored
Merge pull request #13 from alecmocatta/ondrop
Add on_return_or_unwind
2 parents 840b826 + f110470 commit c3730ae

File tree

4 files changed

+103
-92
lines changed

4 files changed

+103
-92
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "replace_with"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
license = "MIT OR Apache-2.0"
55
authors = ["Alec Mocatta <[email protected]>"]
66
categories = ["rust-patterns"]
@@ -10,12 +10,12 @@ Temporarily take ownership of a value at a mutable location, and replace it with
1010
"""
1111
repository = "https://github.com/alecmocatta/replace_with"
1212
homepage = "https://github.com/alecmocatta/replace_with"
13-
documentation = "https://docs.rs/replace_with/0.1.5"
13+
documentation = "https://docs.rs/replace_with"
1414
readme = "README.md"
1515
edition = "2018"
1616

1717
[badges]
18-
azure-devops = { project = "alecmocatta/replace_with", pipeline = "tests" }
18+
azure-devops = { project = "alecmocatta/replace_with", pipeline = "tests", build = "11" }
1919
maintenance = { status = "actively-developed" }
2020

2121
[features]

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
[![Crates.io](https://img.shields.io/crates/v/replace_with.svg?maxAge=86400)](https://crates.io/crates/replace_with)
44
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/replace_with.svg?maxAge=2592000)](#License)
5-
[![Build Status](https://dev.azure.com/alecmocatta/replace_with/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/replace_with/_build/latest?branchName=master)
5+
[![Build Status](https://dev.azure.com/alecmocatta/replace_with/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/replace_with/_build?definitionId=11)
66

7-
[Docs](https://docs.rs/replace_with/0.1.5)
7+
[📖 Docs](https://docs.rs/replace_with) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects)
88

99
Temporarily take ownership of a value at a mutable location, and replace it with a new value based on the old one.
1010

11-
This crate provides the function [`replace_with()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with.html), which is like [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html) except it allows the replacement value to be mapped from the original value.
11+
This crate provides the function [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html), which is like [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html) except it allows the replacement value to be mapped from the original value.
1212

1313
See [RFC 1736](https://github.com/rust-lang/rfcs/pull/1736) for a lot of discussion as to its merits. It was never merged, and the desired ability to temporarily move out of `&mut T` doesn't exist yet, so this crate is my interim solution.
1414

15-
It's very akin to [`take_mut`](https://github.com/Sgeo/take_mut), though uses `Drop` instead of [`std::panic::catch_unwind()`](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) to react to unwinding, which avoids the optimisation barrier of calling the `extern "C" __rust_maybe_catch_panic()`. As such it's up to ∞x faster. The API also attempts to make slightly more explicit the behavior on panic – [`replace_with()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with.html) accepts two closures such that aborting in the "standard case" where the mapping closure (`FnOnce(T) -> T`) panics (as [`take_mut::take()`](https://docs.rs/take_mut/0.2.2/take_mut/fn.take.html) does) is avoided. If the second closure (`FnOnce() -> T`) panics, however, then it does indeed abort. The "abort on first panic" behaviour is available with [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with_or_abort.html).
15+
It's very akin to [`take_mut`](https://github.com/Sgeo/take_mut), though uses `Drop` instead of [`std::panic::catch_unwind()`](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) to react to unwinding, which avoids the optimisation barrier of calling the `extern "C" __rust_maybe_catch_panic()`. As such it's up to ∞x faster. The API also attempts to make slightly more explicit the behavior on panic – [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html) accepts two closures such that aborting in the "standard case" where the mapping closure (`FnOnce(T) -> T`) panics (as [`take_mut::take()`](https://docs.rs/take_mut/0.2.2/take_mut/fn.take.html) does) is avoided. If the second closure (`FnOnce() -> T`) panics, however, then it does indeed abort. The "abort on first panic" behaviour is available with [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort.html).
1616

1717
## Example
1818

@@ -70,9 +70,9 @@ features = []
7070
...
7171
```
7272

73-
The [`replace_with()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with.html) & [`replace_with_or_default()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with_or_default.html) functions are available on stable Rust both, with and without `std`.
73+
The [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html) & [`replace_with_or_default()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_default.html) functions are available on stable Rust both, with and without `std`.
7474

75-
The [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with_or_abort.html) function however by default makes use of [`std::process::abort()`](https://doc.rust-lang.org/std/process/fn.abort.html) which is not available with `no_std`.
75+
The [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort.html) function however by default makes use of [`std::process::abort()`](https://doc.rust-lang.org/std/process/fn.abort.html) which is not available with `no_std`.
7676

7777
As such `replace_with` will by default call [`core::intrinsics::abort()`](https://doc.rust-lang.org/core/intrinsics/fn.abort.html) instead, which in turn requires nightly Rust.
7878

@@ -102,7 +102,7 @@ features = ["panic_abort"]
102102
...
103103
```
104104

105-
… the `"panic_abort"` feature enables the [`replace_with_or_abort_unchecked()`](https://docs.rs/replace_with/0.1.5/replace_with/fn.replace_with_or_abort_unchecked.html) function becomes on stable Rust as an `unsafe` function, a simple wrapper around `ptr::write(dest, f(ptr::read(dest)));`.
105+
… the `"panic_abort"` feature enables the [`replace_with_or_abort_unchecked()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort_unchecked.html) function becomes on stable Rust as an `unsafe` function, a simple wrapper around `ptr::write(dest, f(ptr::read(dest)));`.
106106

107107
**Word of caution:** It is crucial to only ever use this function having defined `panic = "abort"`, or else bad things may happen. It's *up to you* to uphold this invariant!
108108

azure-pipelines.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ jobs:
1414
endpoint: alecmocatta
1515
default:
1616
rust_toolchain: 1.21.0 stable beta nightly
17-
rust_lint_toolchain: nightly-2019-07-19
17+
rust_lint_toolchain: nightly-2020-07-12
1818
rust_flags: ''
1919
rust_features: ';default;all'
2020
rust_target_check: ''
2121
rust_target_build: ''
2222
rust_target_run: ''
2323
matrix:
2424
windows:
25-
imageName: 'vs2017-win2016'
25+
imageName: 'windows-latest'
2626
rust_target_run: 'x86_64-pc-windows-msvc'
2727
mac:
28-
imageName: 'macos-10.13'
28+
imageName: 'macos-latest'
2929
rust_target_run: 'x86_64-apple-darwin'
3030
linux:
31-
imageName: 'ubuntu-16.04'
32-
rust_target_run: 'x86_64-unknown-linux-gnu x86_64-unknown-linux-musl'
31+
imageName: 'ubuntu-latest'
32+
rust_target_run: 'x86_64-unknown-linux-gnu'

0 commit comments

Comments
 (0)