Skip to content

Commit 2d625ac

Browse files
committed
Rename "abort" to "exit", because abort() is not actually being called.
1 parent 76a52ce commit 2d625ac

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "take_mut"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Sgeo <[email protected]>"]
55
license = "MIT"
66
homepage = "https://github.com/Sgeo/take_mut"
77
repository = "https://github.com/Sgeo/take_mut"
88
description = "Take a T from a &mut T temporarily"
9-
documentation = "https://crates.fyi/crates/take_mut/0.1.0/"
9+
documentation = "https://crates.fyi/crates/take_mut/0.1.1/"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This crate provides (at this time) a single function, `take()`.
44

55
`take()` allows for taking `T` out of a `&mut T`, doing anything with it including consuming it, and producing another `T` to put back in the `&mut T`.
66

7-
During `take()`, if a panic occurs, the entire process will be aborted, as there's no valid `T` to put back into the `&mut T`.
7+
During `take()`, if a panic occurs, the entire process will be exited, as there's no valid `T` to put back into the `&mut T`.
88

99
Contrast with `std::mem::replace()`, which allows for putting a different `T` into a `&mut T`, but requiring the new `T` to be available before being able to consume the old `T`.
1010

src/abort_on_panic.rs

-35
This file was deleted.

src/exit_on_panic.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Used to ensure an exit on panic.
2+
/// Call .done() to consume without exiting
3+
#[derive(Debug)]
4+
struct ExitOnSuddenDrop {
5+
finished: bool
6+
}
7+
8+
impl ExitOnSuddenDrop {
9+
10+
pub fn new() -> Self {
11+
ExitOnSuddenDrop { finished: false }
12+
}
13+
/// Consume `self` without exiting
14+
pub fn done(mut self) {
15+
self.finished = true;
16+
}
17+
}
18+
19+
impl Drop for ExitOnSuddenDrop {
20+
fn drop(&mut self) {
21+
if !self.finished {
22+
::std::process::exit(-1);
23+
}
24+
}
25+
}
26+
27+
28+
/// Calls its closure.
29+
/// If the closure panics, kill the process.
30+
pub fn exit_on_panic<R, F: FnOnce() -> R>(f: F) -> R {
31+
let exiter = ExitOnSuddenDrop::new();
32+
let result = f();
33+
exiter.done();
34+
result
35+
}

src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
//!
33
//! `take()` allows for taking `T` out of a `&mut T`, doing anything with it including consuming it, and producing another `T` to put back in the `&mut T`.
44
//!
5-
//! During `take()`, if a panic occurs, the entire process will be aborted, as there's no valid `T` to put back into the `&mut T`.
5+
//! During `take()`, if a panic occurs, the entire process will be exited, as there's no valid `T` to put back into the `&mut T`.
66
//!
77
//! Contrast with `std::mem::replace()`, which allows for putting a different `T` into a `&mut T`, but requiring the new `T` to be available before being able to consume the old `T`.
88
9-
mod abort_on_panic;
9+
mod exit_on_panic;
1010

11-
use abort_on_panic::abort_on_panic;
11+
use exit_on_panic::exit_on_panic;
1212

1313
/// Allows use of a value pointed to by `&mut T` as though it was owned, as long as a `T` is made available afterwards.
1414
///
1515
/// The closure must return a valid T.
16-
/// # Aborts
17-
/// Will abort the program (exiting with status code -1) if the closure panics.
16+
/// # Important
17+
/// Will exit the program (with status code -1) if the closure panics.
1818
///
1919
/// # Example
2020
/// ```
@@ -30,7 +30,7 @@ use abort_on_panic::abort_on_panic;
3030
pub fn take<T, F>(mut_ref: &mut T, closure: F)
3131
where F: FnOnce(T) -> T {
3232
use std::ptr;
33-
abort_on_panic(|| {
33+
exit_on_panic(|| {
3434
unsafe {
3535
let old_t = ptr::read(mut_ref);
3636
let new_t = closure(old_t);

0 commit comments

Comments
 (0)