-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🚧 try adding AsyncComparer to replace async-trait
- Loading branch information
1 parent
9c6992f
commit 781f0d1
Showing
6 changed files
with
126 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{ | ||
future::Future, | ||
io, | ||
marker::PhantomPinned, | ||
pin::Pin, | ||
task::{ready, Context, Poll}, | ||
}; | ||
|
||
use pin_project::pin_project; | ||
use tokio::io::AsyncRead; | ||
|
||
use super::AsyncComparer; | ||
use crate::CompareResult; | ||
|
||
#[derive(Debug)] | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
#[pin_project] | ||
pub struct Compare<'a, C: ?Sized, Out, Ans> { | ||
comparer: &'a mut C, | ||
out: Out, | ||
ans: Ans, | ||
|
||
// Make this future `!Unpin` for compatibility with async trait methods. | ||
#[pin] | ||
_pin: PhantomPinned, | ||
} | ||
|
||
pub(super) fn compare<'a, C, Out, Ans>( | ||
comparer: &'a mut C, | ||
out: Out, | ||
ans: Ans, | ||
) -> Compare<'a, C, Out, Ans> | ||
where | ||
C: AsyncComparer + Unpin + ?Sized, | ||
Out: AsyncRead + Unpin, | ||
Ans: AsyncRead + Unpin, | ||
{ | ||
Compare { | ||
comparer, | ||
out, | ||
ans, | ||
_pin: PhantomPinned, | ||
} | ||
} | ||
|
||
fn compare_internal<C, Out, Ans>( | ||
mut comparer: Pin<&mut C>, | ||
cx: &mut Context, | ||
out: Out, | ||
ans: Ans, | ||
) -> Poll<io::Result<CompareResult>> | ||
where | ||
C: AsyncComparer + Unpin + ?Sized, | ||
Out: AsyncRead + Unpin, | ||
Ans: AsyncRead + Unpin, | ||
{ | ||
Poll::Ready(ready!(comparer.as_mut().poll_compare(cx, out, ans))) | ||
} | ||
|
||
impl<'c, C, Out, Ans> Future for Compare<'c, C, Out, Ans> | ||
where | ||
C: AsyncComparer + Unpin + ?Sized, | ||
Out: AsyncRead + Unpin, | ||
Ans: AsyncRead + Unpin, | ||
{ | ||
type Output = io::Result<CompareResult>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let me = self.project(); | ||
|
||
compare_internal(Pin::new(*me.comparer), cx, me.out, me.ans) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub mod compare; | ||
use std::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
use tokio::io::{self, AsyncRead}; | ||
|
||
use self::compare::{compare, Compare}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum CompareResult { | ||
Accepted, | ||
WrongAnswer, | ||
PresentationError, | ||
} | ||
|
||
// TODO: Migrate to AsyncComparer trait | ||
#[async_trait] | ||
pub trait Comparer { | ||
async fn compare<Out, Ans>(&self, out: Out, ans: Ans) -> io::Result<CompareResult> | ||
where | ||
Out: AsyncRead + Send + Unpin, | ||
Ans: AsyncRead + Send + Unpin; | ||
} | ||
|
||
pub trait AsyncComparer { | ||
fn poll_compare<Out, Ans>( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context, | ||
out: Out, | ||
ans: Ans, | ||
) -> Poll<io::Result<CompareResult>> | ||
where | ||
Out: AsyncRead + Unpin, | ||
Ans: AsyncRead + Unpin; | ||
|
||
fn compare<'a, Out, Ans>(&'a mut self, out: Out, ans: Ans) -> Compare<'a, Self, Out, Ans> | ||
where | ||
Self: Unpin, | ||
Out: AsyncRead + Send + Unpin, | ||
Ans: AsyncRead + Send + Unpin, | ||
{ | ||
compare(self, out, ans) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,4 @@ | ||
use async_trait::async_trait; | ||
use tokio::io::{self, AsyncRead}; | ||
pub mod comparer; | ||
pub mod default_comparer; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum CompareResult { | ||
Accepted, | ||
WrongAnswer, | ||
PresentationError, | ||
} | ||
|
||
// TODO: Add `Compare` type and mannually implement `Future` trait for it. | ||
#[async_trait] | ||
pub trait Comparer { | ||
async fn compare<Out, Ans>(&self, out: Out, ans: Ans) -> io::Result<CompareResult> | ||
where | ||
Out: AsyncRead + Send + Unpin, | ||
Ans: AsyncRead + Send + Unpin; | ||
} | ||
pub use comparer::{CompareResult, Comparer}; |