mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
Port scoped_push to Rust
This commit is contained in:
parent
844174367b
commit
15d4310ae9
2 changed files with 34 additions and 0 deletions
33
fish-rust/src/common.rs
Normal file
33
fish-rust/src/common.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use std::mem;
|
||||
|
||||
/// A scoped manager to save the current value of some variable, and optionally set it to a new
|
||||
/// value. When dropped, it restores the variable to its old value.
|
||||
///
|
||||
/// This can be handy when there are multiple code paths to exit a block.
|
||||
pub struct ScopedPush<'a, T> {
|
||||
var: &'a mut T,
|
||||
saved_value: Option<T>,
|
||||
}
|
||||
|
||||
impl<'a, T> ScopedPush<'a, T> {
|
||||
pub fn new(var: &'a mut T, new_value: T) -> Self {
|
||||
let saved_value = mem::replace(var, new_value);
|
||||
|
||||
Self {
|
||||
var,
|
||||
saved_value: Some(saved_value),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) {
|
||||
if let Some(saved_value) = self.saved_value.take() {
|
||||
*self.var = saved_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Drop for ScopedPush<'a, T> {
|
||||
fn drop(&mut self) {
|
||||
self.restore()
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
#![allow(clippy::needless_return)]
|
||||
#![allow(clippy::manual_is_ascii_check)]
|
||||
|
||||
mod common;
|
||||
mod fd_readable_set;
|
||||
mod fds;
|
||||
#[allow(rustdoc::broken_intra_doc_links)]
|
||||
|
|
Loading…
Reference in a new issue