diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-10-17 14:38:48 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-10-23 20:23:47 +0200 |
| commit | 5becb32ba463d6b0ace914ab06bb237483a94fbc (patch) | |
| tree | 684efb242ddb04e71c54f9665cc59891f734e518 /src/util/mod.rs | |
| parent | c627847cb39572c08f3b53db07ea325ef0d352fa (diff) | |
Introduce page / block / inline levels
Diffstat (limited to 'src/util/mod.rs')
| -rw-r--r-- | src/util/mod.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs index 05dc9025..c4272c55 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -10,6 +10,7 @@ use std::cell::RefMut; use std::cmp::Ordering; use std::ops::Range; use std::path::{Component, Path, PathBuf}; +use std::rc::Rc; /// Additional methods for booleans. pub trait BoolExt { @@ -67,6 +68,25 @@ impl<T> OptionExt<T> for Option<T> { } } +/// Additional methods for reference-counted pointers. +pub trait RcExt<T> { + /// Takes the inner value if there is exactly one strong reference and + /// clones it otherwise. + fn take(self) -> T; +} + +impl<T> RcExt<T> for Rc<T> +where + T: Clone, +{ + fn take(self) -> T { + match Rc::try_unwrap(self) { + Ok(v) => v, + Err(rc) => (*rc).clone(), + } + } +} + /// Additional methods for slices. pub trait SliceExt<T> { /// Split a slice into consecutive groups with the same key. |
