mod cancel;
mod personality;
use std::borrow::Cow;
pub use cancel::*;
use compio_driver::Extra;
pub use personality::*;
use crate::CancelToken;
#[non_exhaustive]
#[derive(Debug, Default)]
pub(crate) struct Ext<'a> {
personality: Option<u16>,
cancel: Option<Cow<'a, CancelToken>>,
}
impl<'a> Ext<'a> {
pub fn to_owned(&self) -> Ext<'static> {
Ext {
personality: self.personality,
cancel: self
.cancel
.as_ref()
.map(|x| Cow::Owned(x.clone().into_owned())),
}
}
}
impl<'a> Ext<'a> {
pub fn with_personality(&self, personality: u16) -> Self {
Self {
personality: Some(personality),
cancel: self.cancel.clone(),
}
}
pub fn with_cancel(&self, token: &'a CancelToken) -> Self {
Self {
personality: self.personality,
cancel: Some(Cow::Borrowed(token)),
}
}
pub fn get_cancel(&self) -> Option<&CancelToken> {
self.cancel.as_deref()
}
pub fn set_extra(&self, extra: &mut Extra) -> bool {
let mut changed = false;
if let Some(personality) = self.personality {
extra.set_personality(personality);
changed = true;
}
changed
}
}
pub trait FutureExt {
fn with_personality(self, personality: u16) -> WithPersonality<Self>
where
Self: Sized,
{
WithPersonality::new(self, personality)
}
fn with_cancel(self, token: CancelToken) -> WithCancel<Self>
where
Self: Sized,
{
WithCancel::new(self, token)
}
}
impl<F: Future + ?Sized> FutureExt for F {}