Skip to main content

compio_runtime/future/
personality.rs

1use std::{
2    pin::Pin,
3    task::{Context, ContextBuilder, Poll},
4};
5
6use pin_project_lite::pin_project;
7
8use crate::future::Ext;
9
10pin_project! {
11    /// A future with a personality attached to it.
12    pub struct WithPersonality<F: ?Sized> {
13        personality: u16,
14        #[pin]
15        future: F,
16    }
17}
18
19impl<F> WithPersonality<F> {
20    /// Create a new [`WithPersonality`] future.
21    pub fn new(future: F, personality: u16) -> Self {
22        Self {
23            future,
24            personality,
25        }
26    }
27}
28
29impl<F: Future + ?Sized> Future for WithPersonality<F> {
30    type Output = F::Output;
31
32    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
33        let this = self.project();
34        if let Some(ext) = cx.ext().downcast_mut::<Ext>() {
35            ext.set_personality(*this.personality);
36            this.future.poll(cx)
37        } else {
38            let mut ex = Ext::new().with_personality(*this.personality);
39            let mut cx = ContextBuilder::from(cx).ext(&mut ex).build();
40            this.future.poll(&mut cx)
41        }
42    }
43}