diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /third_party/rust/replace_with | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/replace_with')
-rw-r--r-- | third_party/rust/replace_with/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/replace_with/Cargo.toml | 38 | ||||
-rw-r--r-- | third_party/rust/replace_with/LICENSE-APACHE.txt | 201 | ||||
-rw-r--r-- | third_party/rust/replace_with/LICENSE-MIT.txt | 23 | ||||
-rw-r--r-- | third_party/rust/replace_with/README.md | 117 | ||||
-rw-r--r-- | third_party/rust/replace_with/azure-pipelines.yml | 32 | ||||
-rwxr-xr-x | third_party/rust/replace_with/src/lib.rs | 596 |
7 files changed, 1008 insertions, 0 deletions
diff --git a/third_party/rust/replace_with/.cargo-checksum.json b/third_party/rust/replace_with/.cargo-checksum.json new file mode 100644 index 0000000000..d06f44d085 --- /dev/null +++ b/third_party/rust/replace_with/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"c84c56d27d6113b6b1c081aa2377801ed9dc223e63b32de8bfda3d645d601282","LICENSE-APACHE.txt":"8173d5c29b4f956d532781d2b86e4e30f83e6b7878dce18c919451d6ba707c90","LICENSE-MIT.txt":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"2b55e25c35977c9d196ca42883df76ad5c721ea3fa6ef45b9548cc0499d1392d","azure-pipelines.yml":"3c0c697b1584f8933af67fa08478b07e9ce1fb2a4f35ed2058fbbea4bfc45354","src/lib.rs":"476cf27a89c488e68de4813dfbe08a6a5ec765802ab911ca406d1e9a67592b66"},"package":"e3a8614ee435691de62bcffcf4a66d91b3594bf1428a5722e79103249a095690"}
\ No newline at end of file diff --git a/third_party/rust/replace_with/Cargo.toml b/third_party/rust/replace_with/Cargo.toml new file mode 100644 index 0000000000..2fbd957b37 --- /dev/null +++ b/third_party/rust/replace_with/Cargo.toml @@ -0,0 +1,38 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# 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 +# +# 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) + +[package] +edition = "2018" +name = "replace_with" +version = "0.1.7" +authors = ["Alec Mocatta <alec@mocatta.net>"] +description = "Temporarily take ownership of a value at a mutable location, and replace it with a new value based on the old one.\n" +homepage = "https://github.com/alecmocatta/replace_with" +documentation = "https://docs.rs/replace_with" +readme = "README.md" +keywords = ["mutability"] +categories = ["rust-patterns"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/alecmocatta/replace_with" + +[features] +default = ["std"] +nightly = [] +panic_abort = [] +std = [] +[badges.azure-devops] +build = "11" +pipeline = "tests" +project = "alecmocatta/replace_with" + +[badges.maintenance] +status = "actively-developed" diff --git a/third_party/rust/replace_with/LICENSE-APACHE.txt b/third_party/rust/replace_with/LICENSE-APACHE.txt new file mode 100644 index 0000000000..11069edd79 --- /dev/null +++ b/third_party/rust/replace_with/LICENSE-APACHE.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/replace_with/LICENSE-MIT.txt b/third_party/rust/replace_with/LICENSE-MIT.txt new file mode 100644 index 0000000000..31aa79387f --- /dev/null +++ b/third_party/rust/replace_with/LICENSE-MIT.txt @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/replace_with/README.md b/third_party/rust/replace_with/README.md new file mode 100644 index 0000000000..a04c2d98eb --- /dev/null +++ b/third_party/rust/replace_with/README.md @@ -0,0 +1,117 @@ +# replace_with + +[![Crates.io](https://img.shields.io/crates/v/replace_with.svg?maxAge=86400)](https://crates.io/crates/replace_with) +[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/replace_with.svg?maxAge=2592000)](#License) +[![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) + +[📖 Docs](https://docs.rs/replace_with) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects) + +Temporarily take ownership of a value at a mutable location, and replace it with a new value based on the old one. + +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. + +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. + +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). + +## Example + +Consider this motivating example: + +```rust +enum States { + A(String), + B(String), +} + +impl States { + fn poll(&mut self) { + // error[E0507]: cannot move out of borrowed content + *self = match *self { + // ^^^^^ cannot move out of borrowed content + States::A(a) => States::B(a), + States::B(a) => States::A(a), + }; + } +} +``` + +Depending on context this can be quite tricky to work around. With this crate, however: + +```rust +enum States { + A(String), + B(String), +} + +impl States { + fn poll(&mut self) { + replace_with_or_abort(self, |self_| match self_ { + States::A(a) => States::B(a), + States::B(a) => States::A(a), + }); + } +} +``` + +Huzzah! + +## `no_std` + +To use `replace_with` with `no_std` you have to disable the `std` feature, which is active by default, by specifying your dependency to it like this: + +```toml +# Cargo.toml + +[dependencies.replace_with] +version = ... +default-features = false +features = [] +... +``` + +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`. + +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`. + +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. + +Not everything is lost for stable `no_std` though, `replace_with` has one more trick up its sleeve: + +### panic = "abort" + +If you define [`panic = abort`](https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/aborting-on-panic.html) in the `[profile]` section of your crate's `Cargo.toml` … + +```toml +# Cargo.toml + +[profile.debug] +panic = "abort" + +[profile.release] +panic = "abort" +``` + +… and add the `"panic_abort"` feature to `replace_with` in the `dependencies` section of your crate's `Cargo.toml` … + +```toml +# Cargo.toml + +[dependencies.replace_with] +features = ["panic_abort"] +... +``` + +… 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)));`. + +**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! + +## License +Licensed under either of + + * Apache License, Version 2.0, ([LICENSE-APACHE.txt](LICENSE-APACHE.txt) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT.txt](LICENSE-MIT.txt) or http://opensource.org/licenses/MIT) + +at your option. + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/third_party/rust/replace_with/azure-pipelines.yml b/third_party/rust/replace_with/azure-pipelines.yml new file mode 100644 index 0000000000..133937a0d3 --- /dev/null +++ b/third_party/rust/replace_with/azure-pipelines.yml @@ -0,0 +1,32 @@ +trigger: ["master"] +pr: ["master"] + +resources: + repositories: + - repository: templates + type: github + name: alecmocatta/azure-pipeline-templates + endpoint: alecmocatta + +jobs: +- template: rust.yml@templates + parameters: + endpoint: alecmocatta + default: + rust_toolchain: 1.21.0 stable beta nightly + rust_lint_toolchain: nightly-2020-07-12 + rust_flags: '' + rust_features: ';default;all' + rust_target_check: '' + rust_target_build: '' + rust_target_run: '' + matrix: + windows: + imageName: 'windows-latest' + rust_target_run: 'x86_64-pc-windows-msvc' + mac: + imageName: 'macos-latest' + rust_target_run: 'x86_64-apple-darwin' + linux: + imageName: 'ubuntu-latest' + rust_target_run: 'x86_64-unknown-linux-gnu' diff --git a/third_party/rust/replace_with/src/lib.rs b/third_party/rust/replace_with/src/lib.rs new file mode 100755 index 0000000000..05ab0fbf04 --- /dev/null +++ b/third_party/rust/replace_with/src/lib.rs @@ -0,0 +1,596 @@ +//! Temporarily take ownership of a value at a mutable location, and replace it with a new value +//! based on the old one. +//! +//! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong> +//! <a href="https://crates.io/crates/replace_with">📦 Crates.io</a> │ <a href="https://github.com/alecmocatta/replace_with">📑 GitHub</a> │ <a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬 Chat</a> +//! </strong></p> +//! +//! This crate provides the function [`replace_with()`], which is like [`std::mem::replace()`] +//! except it allows the replacement value to be mapped from the original value. +//! +//! 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. +//! +//! It's very akin to [`take_mut`](https://github.com/Sgeo/take_mut), though uses `Drop` instead of +//! [`std::panic::catch_unwind()`] 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()`] 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()`]. +//! +//! # Example +//! +//! Consider this motivating example: +//! +//! ```compile_fail +//! # use replace_with::*; +//! enum States { +//! A(String), +//! B(String), +//! } +//! +//! impl States { +//! fn poll(&mut self) { +//! // error[E0507]: cannot move out of borrowed content +//! *self = match *self { +//! // ^^^^^ cannot move out of borrowed content +//! States::A(a) => States::B(a), +//! States::B(a) => States::A(a), +//! }; +//! } +//! } +//! ``` +//! +//! Depending on context this can be quite tricky to work around. With this crate, however: +//! +//! ``` +//! # use replace_with::*; +//! enum States { +//! A(String), +//! B(String), +//! } +//! +//! # #[cfg(any(feature = "std", feature = "nightly"))] +//! impl States { +//! fn poll(&mut self) { +//! replace_with_or_abort(self, |self_| match self_ { +//! States::A(a) => States::B(a), +//! States::B(a) => States::A(a), +//! }); +//! } +//! } +//! ``` +//! +//! Huzzah! + +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr( + all(not(feature = "std"), feature = "nightly"), + feature(core_intrinsics) +)] +#![doc(html_root_url = "https://docs.rs/replace_with/0.1.7")] +#![warn( + missing_copy_implementations, + missing_debug_implementations, + missing_docs, + trivial_casts, + trivial_numeric_casts, + unused_import_braces, + unused_qualifications, + unused_results, + // clippy::pedantic +)] +// #![allow(clippy::inline_always)] + +#[cfg(not(feature = "std"))] +use core as std; + +use std::{mem, ptr}; + +struct OnDrop<F: FnOnce()>(mem::ManuallyDrop<F>); +impl<F: FnOnce()> Drop for OnDrop<F> { + #[inline(always)] + fn drop(&mut self) { + (unsafe { ptr::read(&*self.0) })(); + } +} + +#[doc(hidden)] +#[inline(always)] +pub fn on_unwind<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> T { + let x = OnDrop(mem::ManuallyDrop::new(p)); + let t = f(); + let mut x = mem::ManuallyDrop::new(x); + unsafe { mem::ManuallyDrop::drop(&mut x.0) }; + t +} + +#[doc(hidden)] +#[inline(always)] +pub fn on_return_or_unwind<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> T { + let _x = OnDrop(mem::ManuallyDrop::new(p)); + f() +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, `default` will be called to +/// provide a replacement value. `default` should not panic – doing so will constitute a double +/// panic and will most likely abort the process. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// enum States { +/// A(String), +/// B(String), +/// } +/// +/// impl States { +/// fn poll(&mut self) { +/// replace_with( +/// self, +/// || States::A(String::new()), +/// |self_| match self_ { +/// States::A(a) => States::B(a), +/// States::B(a) => States::A(a), +/// }, +/// ); +/// } +/// } +/// ``` +#[inline] +pub fn replace_with<T, D: FnOnce() -> T, F: FnOnce(T) -> T>(dest: &mut T, default: D, f: F) { + unsafe { + let old = ptr::read(dest); + let new = on_unwind(move || f(old), || ptr::write(dest, default())); + ptr::write(dest, new); + } +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Replaces with [`Default::default()`] on panic. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, `T::default()` will be called to +/// provide a replacement value. `T::default()` should not panic – doing so will constitute a double +/// panic and will most likely abort the process. +/// +/// Equivalent to `replace_with(dest, T::default, f)`. +/// +/// Differs from `*dest = mem::replace(dest, Default::default())` in that `Default::default()` will +/// only be called on panic. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// enum States { +/// A(String), +/// B(String), +/// } +/// +/// impl Default for States { +/// fn default() -> Self { +/// States::A(String::new()) +/// } +/// } +/// +/// impl States { +/// fn poll(&mut self) { +/// replace_with_or_default(self, |self_| match self_ { +/// States::A(a) => States::B(a), +/// States::B(a) => States::A(a), +/// }); +/// } +/// } +/// ``` +#[inline] +pub fn replace_with_or_default<T: Default, F: FnOnce(T) -> T>(dest: &mut T, f: F) { + replace_with(dest, T::default, f); +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Aborts on panic. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to +/// avoid returning control while `dest` is in a potentially invalid state. +/// +/// If this behaviour is undesirable, use [`replace_with`] or [`replace_with_or_default`]. +/// +/// Equivalent to `replace_with(dest, || process::abort(), f)`. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// enum States { +/// A(String), +/// B(String), +/// } +/// +/// # #[cfg(any(feature = "std", feature = "nightly"))] +/// impl States { +/// fn poll(&mut self) { +/// replace_with_or_abort(self, |self_| match self_ { +/// States::A(a) => States::B(a), +/// States::B(a) => States::A(a), +/// }); +/// } +/// } +/// ``` +#[inline] +#[cfg(feature = "std")] +pub fn replace_with_or_abort<T, F: FnOnce(T) -> T>(dest: &mut T, f: F) { + replace_with(dest, || std::process::abort(), f); +} + +#[inline] +#[cfg(all(not(feature = "std"), feature = "nightly"))] +pub fn replace_with_or_abort<T, F: FnOnce(T) -> T>(dest: &mut T, f: F) { + replace_with(dest, || unsafe { std::intrinsics::abort() }, f); +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Aborts on panic. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to +/// avoid returning control while `dest` is in a potentially invalid state. +/// +/// Unlike for `replace_with_or_abort()` users of `replace_with_or_abort_unchecked()` are expected +/// to have `features = ["panic_abort", …]` defined in `Cargo.toml` +/// and `panic = "abort"` defined in their profile for it to behave semantically correct: +/// +/// ```toml +/// # Cargo.toml +/// +/// [profile.debug] +/// panic = "abort" +/// +/// [profile.release] +/// panic = "abort" +/// ``` +/// +/// # Safety +/// +/// 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! +/// +/// If this behaviour is undesirable, use [`replace_with`] or [`replace_with_or_default`]. +/// +/// Equivalent to `replace_with(dest, || process::abort(), f)`. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// enum States { +/// A(String), +/// B(String), +/// } +/// +/// impl States { +/// fn poll(&mut self) { +/// unsafe { +/// replace_with_or_abort_unchecked(self, |self_| match self_ { +/// States::A(a) => States::B(a), +/// States::B(a) => States::A(a), +/// }); +/// } +/// } +/// } +/// ``` +/// +#[inline] +#[cfg(feature = "panic_abort")] +pub unsafe fn replace_with_or_abort_unchecked<T, F: FnOnce(T) -> T>(dest: &mut T, f: F) { + ptr::write(dest, f(ptr::read(dest))); +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Lets the closure return a custom value as well. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// This is effectively the same function as [`replace_with`], but it lets the closure return a +/// custom value as well. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, `default` will be called to +/// provide a replacement value. `default` should not panic – doing so will constitute a double +/// panic and will most likely abort the process. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// +/// fn take<T>(option: &mut Option<T>) -> Option<T> { +/// replace_with_and_return(option, || None, |option| (option, None)) +/// } +/// +/// let mut opt = Some(3); +/// assert_eq!(take(&mut opt), Some(3)); +/// assert_eq!(take(&mut opt), None); +/// ``` +#[inline] +pub fn replace_with_and_return<T, U, D: FnOnce() -> T, F: FnOnce(T) -> (U, T)>( + dest: &mut T, default: D, f: F, +) -> U { + unsafe { + let old = ptr::read(dest); + let (res, new) = on_unwind(move || f(old), || ptr::write(dest, default())); + ptr::write(dest, new); + res + } +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Replaces with [`Default::default()`] on panic. +/// Lets the closure return a custom value as well. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// This is effectively the same function as [`replace_with_or_default`], but it lets the closure +/// return a custom value as well. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, `T::default()` will be called to +/// provide a replacement value. `T::default()` should not panic – doing so will constitute a double +/// panic and will most likely abort the process. +/// +/// Equivalent to `replace_with_and_return(dest, T::default, f)`. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// +/// fn take<T>(option: &mut Option<T>) -> Option<T> { +/// replace_with_or_default_and_return(option, |option| (option, None)) +/// } +/// ``` +#[inline] +pub fn replace_with_or_default_and_return<T: Default, U, F: FnOnce(T) -> (U, T)>( + dest: &mut T, f: F, +) -> U { + replace_with_and_return(dest, T::default, f) +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Aborts on panic. Lets the closure return a custom value as well. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// This is effectively the same function as [`replace_with_or_abort`], but it lets the closure +/// return a custom value as well. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to +/// avoid returning control while `dest` is in a potentially invalid state. +/// +/// If this behaviour is undesirable, use [`replace_with_and_return`] or +/// [`replace_with_or_default_and_return`] instead. +/// +/// Equivalent to `replace_with_and_return(dest, || process::abort(), f)`. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// +/// fn take<T>(option: &mut Option<T>) -> Option<T> { +/// replace_with_or_abort_and_return(option, |option| (option, None)) +/// } +/// ``` +#[inline] +#[cfg(feature = "std")] +pub fn replace_with_or_abort_and_return<T, U, F: FnOnce(T) -> (U, T)>(dest: &mut T, f: F) -> U { + replace_with_and_return(dest, || std::process::abort(), f) +} + +#[inline] +#[cfg(all(not(feature = "std"), feature = "nightly"))] +pub fn replace_with_or_abort_and_return<T, U, F: FnOnce(T) -> (U, T)>(dest: &mut T, f: F) -> U { + replace_with_and_return(dest, || unsafe { std::intrinsics::abort() }, f) +} + +/// Temporarily takes ownership of a value at a mutable location, and replace it with a new value +/// based on the old one. Aborts on panic. Lets the closure return a custom value as well. +/// +/// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which +/// is then placed at the original value's location. +/// +/// This is effectively the same function as [`replace_with_or_abort_unchecked`], but it lets the +/// closure return a custom value as well. +/// +/// # An important note +/// +/// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to +/// avoid returning control while `dest` is in a potentially invalid state. +/// +/// Unlike for `replace_with_or_abort()` users of `replace_with_or_abort_unchecked()` are expected +/// to have `features = ["panic_abort", …]` defined in `Cargo.toml` +/// and `panic = "abort"` defined in their profile for it to behave semantically correct: +/// +/// ```toml +/// # Cargo.toml +/// +/// [profile.debug] +/// panic = "abort" +/// +/// [profile.release] +/// panic = "abort" +/// ``` +/// +/// # Safety +/// +/// 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! +/// +/// If this behaviour is undesirable, use [`replace_with_and_return`] or +/// [`replace_with_or_default_and_return`]. +/// +/// Equivalent to `replace_with_and_return(dest, || process::abort(), f)`. +/// +/// # Example +/// +/// ``` +/// # use replace_with::*; +/// +/// unsafe fn take<T>(option: &mut Option<T>) -> Option<T> { +/// replace_with_or_abort_and_return_unchecked(option, |option| (option, None)) +/// } +/// ``` +#[inline] +#[cfg(feature = "std")] +#[cfg(feature = "panic_abort")] +pub unsafe fn replace_with_or_abort_and_return_unchecked<T, U, F: FnOnce(T) -> (U, T)>( + dest: &mut T, f: F, +) -> U { + let (res, new) = f(ptr::read(dest)); + ptr::write(dest, new); + res +} + +#[cfg(test)] +mod test { + // These functions copied from https://github.com/Sgeo/take_mut/blob/1bd70d842c6febcd16ec1fe3a954a84032b89f52/src/lib.rs#L102-L147 + + // Copyright (c) 2016 Sgeo + + // Permission is hereby granted, free of charge, to any person obtaining a copy + // of this software and associated documentation files (the "Software"), to deal + // in the Software without restriction, including without limitation the rights + // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + // copies of the Software, and to permit persons to whom the Software is + // furnished to do so, subject to the following conditions: + + // The above copyright notice and this permission notice shall be included in all + // copies or substantial portions of the Software. + + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + // SOFTWARE. + + use super::*; + + #[test] + fn it_works_recover() { + #[derive(PartialEq, Eq, Debug)] + enum Foo { + A, + B, + }; + impl Drop for Foo { + #[cfg(feature = "std")] + fn drop(&mut self) { + match *self { + Foo::A => println!("Foo::A dropped"), + Foo::B => println!("Foo::B dropped"), + } + } + + #[cfg(not(feature = "std"))] + fn drop(&mut self) { + match *self { + Foo::A | Foo::B => (), + } + } + } + let mut quax = Foo::A; + replace_with( + &mut quax, + || Foo::A, + |f| { + drop(f); + Foo::B + }, + ); + assert_eq!(&quax, &Foo::B); + + let n = replace_with_and_return( + &mut quax, + || Foo::B, + |f| { + drop(f); + (3, Foo::A) + }, + ); + assert_eq!(n, 3); + assert_eq!(&quax, &Foo::A); + } + + #[cfg(all(feature = "std", not(miri)))] // https://github.com/rust-lang/miri/issues/658 + #[test] + fn it_works_recover_panic() { + use std::panic; + + #[derive(PartialEq, Eq, Debug)] + enum Foo { + A, + B, + C, + }; + impl Drop for Foo { + fn drop(&mut self) { + match *self { + Foo::A => println!("Foo::A dropped"), + Foo::B => println!("Foo::B dropped"), + Foo::C => println!("Foo::C dropped"), + } + } + } + let mut quax = Foo::A; + + let res = panic::catch_unwind(panic::AssertUnwindSafe(|| { + replace_with( + &mut quax, + || Foo::C, + |f| { + drop(f); + panic!("panic"); + #[allow(unreachable_code)] + Foo::B + }, + ); + })); + + assert!(res.is_err()); + assert_eq!(&quax, &Foo::C); + } +} |