pub struct DropGuard<T, F>where
F: FnOnce(T),{ /* private fields */ }
drop_guard
)Expand description
Wrap a value and run a closure when dropped.
This is useful for quickly creating destructors inline.
ยงExamples
#![feature(drop_guard)]
use std::mem::DropGuard;
{
// Create a new guard around a string that will
// print its value when dropped.
let s = String::from("Chashu likes tuna");
let mut s = DropGuard::new(s, |s| println!("{s}"));
// Modify the string contained in the guard.
s.push_str("!!!");
// The guard will be dropped here, printing:
// "Chashu likes tuna!!!"
}
Implementationsยง
Sourceยงimpl<T, F> DropGuard<T, F>where
F: FnOnce(T),
impl<T, F> DropGuard<T, F>where
F: FnOnce(T),
Sourcepub const fn new(inner: T, f: F) -> DropGuard<T, F>
๐ฌThis is a nightly-only experimental API. (drop_guard
)
pub const fn new(inner: T, f: F) -> DropGuard<T, F>
drop_guard
)Create a new instance of DropGuard
.
ยงExample
#![feature(drop_guard)]
use std::mem::DropGuard;
let value = String::from("Chashu likes tuna");
let guard = DropGuard::new(value, |s| println!("{s}"));
Sourcepub fn into_inner(guard: DropGuard<T, F>) -> T
๐ฌThis is a nightly-only experimental API. (drop_guard
)
pub fn into_inner(guard: DropGuard<T, F>) -> T
drop_guard
)Consumes the DropGuard
, returning the wrapped value.
This will not execute the closure. This is implemented as an associated
function to prevent any potential conflicts with any other methods called
into_inner
from the Deref
and DerefMut
impls.
It is typically preferred to call this function instead of mem::forget
because it will return the stored value and drop variables captured
by the closure instead of leaking their owned resources.
ยงExample
#![feature(drop_guard)]
use std::mem::DropGuard;
let value = String::from("Nori likes chicken");
let guard = DropGuard::new(value, |s| println!("{s}"));
assert_eq!(DropGuard::into_inner(guard), "Nori likes chicken");
Trait Implementationsยง
Auto Trait Implementationsยง
impl<T, F> Freeze for DropGuard<T, F>
impl<T, F> RefUnwindSafe for DropGuard<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for DropGuard<T, F>
impl<T, F> Sync for DropGuard<T, F>
impl<T, F> Unpin for DropGuard<T, F>
impl<T, F> UnwindSafe for DropGuard<T, F>where
T: UnwindSafe,
F: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Sourceยงfn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
ยงExample
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Sourceยงfn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Sourceยงfn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Sourceยงfn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Sourceยงfn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Sourceยงfn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Sourceยงfn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Sourceยงfn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Sourceยงfn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Sourceยงfn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
ยงExample
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Sourceยงfn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Sourceยงfn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Sourceยงfn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Sourceยงfn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Sourceยงfn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Sourceยงfn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Sourceยงfn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Sourceยงfn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Sourceยงfn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Sourceยงfn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Sourceยงfn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
ยงExample
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Sourceยงfn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Sourceยงfn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
ยงExample
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Sourceยงfn clear(&self) -> Painted<&T>
๐Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Sourceยงfn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
ยงExample
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
Sourceยงimpl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Sourceยงfn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSourceยงfn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Sourceยงfn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Sourceยงfn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.