2024-06-09 14:02:32 +08:00
|
|
|
use allo_isolate::{IntoDart, Isolate};
|
2024-07-15 15:23:23 +08:00
|
|
|
use anyhow::anyhow;
|
2024-06-09 14:02:32 +08:00
|
|
|
use futures::Sink;
|
|
|
|
use pin_project::pin_project;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
|
|
#[pin_project]
|
|
|
|
pub struct IsolateSink {
|
|
|
|
isolate: Isolate,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IsolateSink {
|
|
|
|
pub fn new(isolate: Isolate) -> Self {
|
|
|
|
Self { isolate }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Sink<T> for IsolateSink
|
|
|
|
where
|
|
|
|
T: IntoDart,
|
|
|
|
{
|
2024-07-15 15:23:23 +08:00
|
|
|
type Error = anyhow::Error;
|
2024-06-09 14:02:32 +08:00
|
|
|
|
|
|
|
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
|
|
|
|
let this = self.project();
|
|
|
|
if this.isolate.post(item) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-07-15 15:23:23 +08:00
|
|
|
Err(anyhow!("failed to post message"))
|
2024-06-09 14:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
}
|