logiguard fork: GPUI xdg-activation keyboard-focus serial fix
Some checks failed
Update All Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
Triage Project Sync (#84) / Sync triage project (push) Has been cancelled
release_nightly / notify_on_failure (push) Has been cancelled
release_nightly / check_style (push) Has been cancelled
release_nightly / run_tests_windows (push) Has been cancelled
release_nightly / clippy_windows (push) Has been cancelled
release_nightly / bundle_linux_aarch64 (push) Has been cancelled
release_nightly / bundle_linux_x86_64 (push) Has been cancelled
release_nightly / bundle_mac_aarch64 (push) Has been cancelled
release_nightly / bundle_mac_x86_64 (push) Has been cancelled
release_nightly / bundle_windows_aarch64 (push) Has been cancelled
release_nightly / bundle_windows_x86_64 (push) Has been cancelled
release_nightly / build_nix_linux_x86_64 (push) Has been cancelled
release_nightly / build_nix_mac_aarch64 (push) Has been cancelled
release_nightly / update_nightly_tag (push) Has been cancelled
Hotfix Review Monitor / check-hotfix-reviews (push) Has been cancelled
Stale PR Review Reminder / check-stale-prs (push) Has been cancelled
Update Weekly Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
Bump collab-staging Tag / update-collab-staging-tag (push) Has been cancelled
compliance_check / scheduled_compliance_check (push) Has been cancelled
Some checks failed
Update All Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
Triage Project Sync (#84) / Sync triage project (push) Has been cancelled
release_nightly / notify_on_failure (push) Has been cancelled
release_nightly / check_style (push) Has been cancelled
release_nightly / run_tests_windows (push) Has been cancelled
release_nightly / clippy_windows (push) Has been cancelled
release_nightly / bundle_linux_aarch64 (push) Has been cancelled
release_nightly / bundle_linux_x86_64 (push) Has been cancelled
release_nightly / bundle_mac_aarch64 (push) Has been cancelled
release_nightly / bundle_mac_x86_64 (push) Has been cancelled
release_nightly / bundle_windows_aarch64 (push) Has been cancelled
release_nightly / bundle_windows_x86_64 (push) Has been cancelled
release_nightly / build_nix_linux_x86_64 (push) Has been cancelled
release_nightly / build_nix_mac_aarch64 (push) Has been cancelled
release_nightly / update_nightly_tag (push) Has been cancelled
Hotfix Review Monitor / check-hotfix-reviews (push) Has been cancelled
Stale PR Review Reminder / check-stale-prs (push) Has been cancelled
Update Weekly Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
Bump collab-staging Tag / update-collab-staging-tag (push) Has been cancelled
compliance_check / scheduled_compliance_check (push) Has been cancelled
Single-commit orphan branch: full zed-industries/zed @ 8c74db0 source tree with a 3-file patch applied (no upstream history). Patch (crates/gpui_linux/src/linux/wayland/): - serial.rs: add SerialKind::KeyboardEnter - client.rs: store wl_keyboard.enter serial; add latest_serial_of() - window.rs: activate() uses keyboard-enter serial (Mutter focus gate) Mutter honors window activation only when the token carries the keyboard- focus serial from wl_keyboard.enter; GPUI used a stale mouse-press serial. See docs/tray-window-focus-wayland.md in logiguard. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
12
crates/gpui_util/Cargo.toml
Normal file
12
crates/gpui_util/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "gpui_util"
|
||||
version = "0.1.0"
|
||||
publish.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
log.workspace = true
|
||||
anyhow.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
1
crates/gpui_util/LICENSE-APACHE
Symbolic link
1
crates/gpui_util/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../../LICENSE-APACHE
|
||||
141
crates/gpui_util/src/arc_cow.rs
Normal file
141
crates/gpui_util/src/arc_cow.rs
Normal file
@@ -0,0 +1,141 @@
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
cmp::Ordering,
|
||||
fmt::{self, Debug},
|
||||
hash::{Hash, Hasher},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub enum ArcCow<'a, T: ?Sized> {
|
||||
Borrowed(&'a T),
|
||||
Owned(Arc<T>),
|
||||
}
|
||||
|
||||
impl<T: ?Sized + PartialEq> PartialEq for ArcCow<'_, T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
let a = self.as_ref();
|
||||
let b = other.as_ref();
|
||||
a == b
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + PartialOrd> PartialOrd for ArcCow<'_, T> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.as_ref().partial_cmp(other.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Ord> Ord for ArcCow<'_, T> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.as_ref().cmp(other.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Eq> Eq for ArcCow<'_, T> {}
|
||||
|
||||
impl<T: ?Sized + Hash> Hash for ArcCow<'_, T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
match self {
|
||||
Self::Borrowed(borrowed) => Hash::hash(borrowed, state),
|
||||
Self::Owned(owned) => Hash::hash(&**owned, state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for ArcCow<'_, T> {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
|
||||
Self::Owned(owned) => Self::Owned(owned.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> From<&'a T> for ArcCow<'a, T> {
|
||||
fn from(s: &'a T) -> Self {
|
||||
Self::Borrowed(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> From<Arc<T>> for ArcCow<'_, T> {
|
||||
fn from(s: Arc<T>) -> Self {
|
||||
Self::Owned(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> From<&'_ Arc<T>> for ArcCow<'_, T> {
|
||||
fn from(s: &'_ Arc<T>) -> Self {
|
||||
Self::Owned(s.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ArcCow<'_, str> {
|
||||
fn from(value: String) -> Self {
|
||||
Self::Owned(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&String> for ArcCow<'_, str> {
|
||||
fn from(value: &String) -> Self {
|
||||
Self::Owned(value.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Cow<'a, str>> for ArcCow<'a, str> {
|
||||
fn from(value: Cow<'a, str>) -> Self {
|
||||
match value {
|
||||
Cow::Borrowed(borrowed) => Self::Borrowed(borrowed),
|
||||
Cow::Owned(owned) => Self::Owned(owned.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Vec<T>> for ArcCow<'_, [T]> {
|
||||
fn from(vec: Vec<T>) -> Self {
|
||||
ArcCow::Owned(Arc::from(vec))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for ArcCow<'a, [u8]> {
|
||||
fn from(s: &'a str) -> Self {
|
||||
ArcCow::Borrowed(s.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'_, T> {
|
||||
fn borrow(&self) -> &T {
|
||||
match self {
|
||||
ArcCow::Borrowed(borrowed) => borrowed,
|
||||
ArcCow::Owned(owned) => owned.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
ArcCow::Borrowed(s) => s,
|
||||
ArcCow::Owned(s) => s.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
|
||||
fn as_ref(&self) -> &T {
|
||||
match self {
|
||||
ArcCow::Borrowed(borrowed) => borrowed,
|
||||
ArcCow::Owned(owned) => owned.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Debug> Debug for ArcCow<'_, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ArcCow::Borrowed(borrowed) => Debug::fmt(borrowed, f),
|
||||
ArcCow::Owned(owned) => Debug::fmt(&**owned, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
393
crates/gpui_util/src/lib.rs
Normal file
393
crates/gpui_util/src/lib.rs
Normal file
@@ -0,0 +1,393 @@
|
||||
// FluentBuilder
|
||||
// pub use gpui_util::{FutureExt, Timeout, arc_cow::ArcCow};
|
||||
|
||||
use std::{
|
||||
env,
|
||||
ops::AddAssign,
|
||||
panic::Location,
|
||||
pin::Pin,
|
||||
sync::OnceLock,
|
||||
task::{Context, Poll},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
pub mod arc_cow;
|
||||
|
||||
pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
|
||||
let prev = *value;
|
||||
*value += T::from(1);
|
||||
prev
|
||||
}
|
||||
|
||||
pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
|
||||
static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
|
||||
let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
|
||||
env::var("ZED_MEASUREMENTS")
|
||||
.map(|measurements| measurements == "1" || measurements == "true")
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
if *zed_measurements {
|
||||
let start = Instant::now();
|
||||
let result = f();
|
||||
let elapsed = start.elapsed();
|
||||
eprintln!("{}: {:?}", label, elapsed);
|
||||
result
|
||||
} else {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! debug_panic {
|
||||
( $($fmt_arg:tt)* ) => {
|
||||
if cfg!(debug_assertions) {
|
||||
panic!( $($fmt_arg)* );
|
||||
} else {
|
||||
let backtrace = std::backtrace::Backtrace::capture();
|
||||
log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn some_or_debug_panic<T>(option: Option<T>) -> Option<T> {
|
||||
#[cfg(debug_assertions)]
|
||||
if option.is_none() {
|
||||
panic!("Unexpected None");
|
||||
}
|
||||
option
|
||||
}
|
||||
|
||||
/// Expands to an immediately-invoked function expression. Good for using the ? operator
|
||||
/// in functions which do not return an Option or Result.
|
||||
///
|
||||
/// Accepts a normal block, an async block, or an async move block.
|
||||
#[macro_export]
|
||||
macro_rules! maybe {
|
||||
($block:block) => {
|
||||
(|| $block)()
|
||||
};
|
||||
(async $block:block) => {
|
||||
(async || $block)()
|
||||
};
|
||||
(async move $block:block) => {
|
||||
(async move || $block)()
|
||||
};
|
||||
}
|
||||
pub trait ResultExt<E> {
|
||||
type Ok;
|
||||
|
||||
fn log_err(self) -> Option<Self::Ok>;
|
||||
/// Like [`ResultExt::log_err`], but uses `{:?}` formatting so `anyhow::Error` values emit their
|
||||
/// full backtrace. Reach for this only when a backtrace is genuinely wanted — most call sites
|
||||
/// should stick with `log_err` / `warn_on_err`, whose output is a single chained error message.
|
||||
fn log_err_with_backtrace(self) -> Option<Self::Ok>
|
||||
where
|
||||
E: std::fmt::Debug;
|
||||
/// Assert that this result should never be an error in development or tests.
|
||||
fn debug_assert_ok(self, reason: &str) -> Self;
|
||||
fn warn_on_err(self) -> Option<Self::Ok>;
|
||||
fn log_with_level(self, level: log::Level) -> Option<Self::Ok>;
|
||||
fn anyhow(self) -> anyhow::Result<Self::Ok>
|
||||
where
|
||||
E: Into<anyhow::Error>;
|
||||
}
|
||||
|
||||
impl<T, E> ResultExt<E> for Result<T, E>
|
||||
where
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
type Ok = T;
|
||||
|
||||
#[track_caller]
|
||||
fn log_err(self) -> Option<T> {
|
||||
self.log_with_level(log::Level::Error)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn log_err_with_backtrace(self) -> Option<T>
|
||||
where
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Ok(value) => Some(value),
|
||||
Err(error) => {
|
||||
log_error_with_caller(
|
||||
*Location::caller(),
|
||||
DebugAsDisplay(&error),
|
||||
log::Level::Error,
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn debug_assert_ok(self, reason: &str) -> Self {
|
||||
if let Err(error) = &self {
|
||||
debug_panic!("{reason} - {error:#}");
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn warn_on_err(self) -> Option<T> {
|
||||
self.log_with_level(log::Level::Warn)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn log_with_level(self, level: log::Level) -> Option<T> {
|
||||
match self {
|
||||
Ok(value) => Some(value),
|
||||
Err(error) => {
|
||||
log_error_with_caller(*Location::caller(), error, level);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn anyhow(self) -> anyhow::Result<T>
|
||||
where
|
||||
E: Into<anyhow::Error>,
|
||||
{
|
||||
self.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
|
||||
where
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
#[cfg(not(windows))]
|
||||
let file = caller.file();
|
||||
#[cfg(windows)]
|
||||
let file = caller.file().replace('\\', "/");
|
||||
// In this codebase all crates reside in a `crates` directory,
|
||||
// so discard the prefix up to that segment to find the crate name
|
||||
let file = file.split_once("crates/");
|
||||
let target = file.as_ref().and_then(|(_, s)| s.split_once("/src/"));
|
||||
|
||||
let module_path = target.map(|(krate, module)| {
|
||||
if module.starts_with(krate) {
|
||||
module.trim_end_matches(".rs").replace('/', "::")
|
||||
} else {
|
||||
krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
|
||||
}
|
||||
});
|
||||
let file = file.map(|(_, file)| format!("crates/{file}"));
|
||||
log::logger().log(
|
||||
&log::Record::builder()
|
||||
.target(module_path.as_deref().unwrap_or(""))
|
||||
.module_path(file.as_deref())
|
||||
.args(format_args!("{:#}", error))
|
||||
.file(Some(caller.file()))
|
||||
.line(Some(caller.line()))
|
||||
.level(level)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn log_err<E: std::fmt::Display>(error: &E) {
|
||||
log_error_with_caller(*Location::caller(), error, log::Level::Error);
|
||||
}
|
||||
|
||||
// Forces `{:?}` formatting through a `Display`-bounded logging helper so `anyhow::Error` emits a
|
||||
// backtrace instead of the single-line chained message produced by its `Display`/`{:#}` forms.
|
||||
struct DebugAsDisplay<'a, E>(&'a E);
|
||||
|
||||
impl<E: std::fmt::Debug> std::fmt::Display for DebugAsDisplay<'_, E> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TryFutureExt {
|
||||
fn log_err(self) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn warn_on_err(self) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
fn unwrap(self) -> UnwrapFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// `{:?}`-formatting companion to [`TryFutureExt`]; emits a backtrace for `anyhow::Error`. Prefer
|
||||
/// [`TryFutureExt`] unless a backtrace is genuinely wanted.
|
||||
pub trait TryFutureExtBacktrace {
|
||||
fn log_err_with_backtrace(self) -> LogErrorWithBacktraceFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn log_tracked_err_with_backtrace(
|
||||
self,
|
||||
location: core::panic::Location<'static>,
|
||||
) -> LogErrorWithBacktraceFuture<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl<F, T, E> TryFutureExt for F
|
||||
where
|
||||
F: Future<Output = Result<T, E>>,
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
#[track_caller]
|
||||
fn log_err(self) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let location = Location::caller();
|
||||
LogErrorFuture(self, log::Level::Error, *location)
|
||||
}
|
||||
|
||||
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
LogErrorFuture(self, log::Level::Error, location)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn warn_on_err(self) -> LogErrorFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let location = Location::caller();
|
||||
LogErrorFuture(self, log::Level::Warn, *location)
|
||||
}
|
||||
|
||||
fn unwrap(self) -> UnwrapFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
UnwrapFuture(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T, E> TryFutureExtBacktrace for F
|
||||
where
|
||||
F: Future<Output = Result<T, E>>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
#[track_caller]
|
||||
fn log_err_with_backtrace(self) -> LogErrorWithBacktraceFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let location = Location::caller();
|
||||
LogErrorWithBacktraceFuture(self, log::Level::Error, *location)
|
||||
}
|
||||
|
||||
fn log_tracked_err_with_backtrace(
|
||||
self,
|
||||
location: core::panic::Location<'static>,
|
||||
) -> LogErrorWithBacktraceFuture<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
LogErrorWithBacktraceFuture(self, log::Level::Error, location)
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
|
||||
|
||||
impl<F, T, E> Future for LogErrorFuture<F>
|
||||
where
|
||||
F: Future<Output = Result<T, E>>,
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
type Output = Option<T>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let level = self.1;
|
||||
let location = self.2;
|
||||
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
|
||||
match inner.poll(cx) {
|
||||
Poll::Ready(output) => Poll::Ready(match output {
|
||||
Ok(output) => Some(output),
|
||||
Err(error) => {
|
||||
log_error_with_caller(location, error, level);
|
||||
None
|
||||
}
|
||||
}),
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub struct LogErrorWithBacktraceFuture<F>(F, log::Level, core::panic::Location<'static>);
|
||||
|
||||
impl<F, T, E> Future for LogErrorWithBacktraceFuture<F>
|
||||
where
|
||||
F: Future<Output = Result<T, E>>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
type Output = Option<T>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let level = self.1;
|
||||
let location = self.2;
|
||||
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
|
||||
match inner.poll(cx) {
|
||||
Poll::Ready(output) => Poll::Ready(match output {
|
||||
Ok(output) => Some(output),
|
||||
Err(error) => {
|
||||
log_error_with_caller(location, DebugAsDisplay(&error), level);
|
||||
None
|
||||
}
|
||||
}),
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UnwrapFuture<F>(F);
|
||||
|
||||
impl<F, T, E> Future for UnwrapFuture<F>
|
||||
where
|
||||
F: Future<Output = Result<T, E>>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
|
||||
match inner.poll(cx) {
|
||||
Poll::Ready(result) => Poll::Ready(result.unwrap()),
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Deferred<F: FnOnce()>(Option<F>);
|
||||
|
||||
impl<F: FnOnce()> Deferred<F> {
|
||||
/// Drop without running the deferred function.
|
||||
pub fn abort(mut self) {
|
||||
self.0.take();
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce()> Drop for Deferred<F> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(f) = self.0.take() {
|
||||
f()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the given function when the returned value is dropped (unless it's cancelled).
|
||||
#[must_use]
|
||||
pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
|
||||
Deferred(Some(f))
|
||||
}
|
||||
Reference in New Issue
Block a user