Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mpz-common): dummy executor #132

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions crates/mpz-common/src/executor/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use async_trait::async_trait;

use scoped_futures::ScopedBoxFuture;
use serio::{Sink, Stream};

use crate::{context::Context, ThreadId};

/// A dummy executor.
#[derive(Debug, Default)]
pub struct DummyExecutor {
id: ThreadId,
io: DummyIo,
}

/// A dummy I/O.
#[derive(Debug, Default)]
pub struct DummyIo;

impl Sink for DummyIo {
type Error = std::io::Error;

fn poll_ready(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
std::task::Poll::Ready(Ok(()))
}

fn start_send<Item: serio::Serialize>(
self: std::pin::Pin<&mut Self>,
_item: Item,
) -> Result<(), Self::Error> {
Ok(())
}

fn poll_flush(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
std::task::Poll::Ready(Ok(()))
}

fn poll_close(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
std::task::Poll::Ready(Ok(()))
}
}

impl Stream for DummyIo {
type Error = std::io::Error;

fn poll_next<Item: serio::Deserialize>(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Result<Item, Self::Error>>> {
std::task::Poll::Ready(None)
}
}

#[async_trait]
impl Context for DummyExecutor {
type Io = DummyIo;

fn id(&self) -> &ThreadId {
&self.id
}

fn io_mut(&mut self) -> &mut Self::Io {
&mut self.io
}

async fn join<'a, A, B, RA, RB>(&'a mut self, a: A, b: B) -> (RA, RB)
where
A: for<'b> FnOnce(&'b mut Self) -> ScopedBoxFuture<'a, 'b, RA> + Send + 'a,
B: for<'b> FnOnce(&'b mut Self) -> ScopedBoxFuture<'a, 'b, RB> + Send + 'a,
RA: Send + 'a,
RB: Send + 'a,
{
let a = a(self).await;
let b = b(self).await;
(a, b)
}

async fn try_join<'a, A, B, RA, RB, E>(&'a mut self, a: A, b: B) -> Result<(RA, RB), E>
where
A: for<'b> FnOnce(&'b mut Self) -> ScopedBoxFuture<'a, 'b, Result<RA, E>> + Send + 'a,
B: for<'b> FnOnce(&'b mut Self) -> ScopedBoxFuture<'a, 'b, Result<RB, E>> + Send + 'a,
RA: Send + 'a,
RB: Send + 'a,
E: Send + 'a,
{
let a = a(self).await?;
let b = b(self).await?;
Ok((a, b))
}
Comment on lines +74 to +97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be polled concurrently? Or doesn't it matter because it is only a dummy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really matter, probably better to do it sequentially to catch any potential deadlocks

}

#[cfg(test)]
mod tests {
use futures::executor::block_on;
use scoped_futures::ScopedFutureExt;

use super::*;

#[derive(Debug, Default)]
struct LifetimeTest {
a: ThreadId,
b: ThreadId,
}

impl LifetimeTest {
// This test is to ensure that the compiler is satisfied with the lifetimes
// of the async closures passed to `join`.
async fn foo<Ctx: Context>(&mut self, ctx: &mut Ctx) {
let a = &mut self.a;
let b = &mut self.b;
ctx.join(
|ctx| {
async move {
*a = ctx.id().clone();
}
.scope_boxed()
},
|ctx| {
async move {
*b = ctx.id().clone();
}
.scope_boxed()
},
)
.await;

// Make sure we can mutate the fields after borrowing them in the async closures.
self.a = ThreadId::default();
self.b = ThreadId::default();
}
}

#[test]
fn test_dummy_executor_join() {
let mut ctx = DummyExecutor::default();
let mut test = LifetimeTest::default();

block_on(test.foo(&mut ctx));
}
}
2 changes: 2 additions & 0 deletions crates/mpz-common/src/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Executors.

mod dummy;
mod st;

pub use dummy::{DummyExecutor, DummyIo};
pub use st::STExecutor;

#[cfg(any(test, feature = "test-utils"))]
Expand Down