Skip to main content

compio_runtime/future/combinator/
personality.rs

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