Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
132 changes: 132 additions & 0 deletions crates/transport-legacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use core::time::Duration;

use std::borrow::Cow;
use std::error::Error;
use std::marker::PhantomData;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context as _};
Expand Down Expand Up @@ -2729,6 +2730,137 @@ impl Encode for Value {
}
}

// Resource Handles
#[derive(Debug)]
pub struct ResourceOwn<T: Send + Sync> {
data: Vec<u8>,
_ty: PhantomData<T>,
}

impl<T: Send + Sync> AsRef<[u8]> for ResourceOwn<T> {
fn as_ref(&self) -> &[u8] {
&self.data
}
}

impl<T: Send + Sync> From<Vec<u8>> for ResourceOwn<T> {
fn from(value: Vec<u8>) -> Self {
Self {
data: value,
_ty: PhantomData,
}
}
}

impl<T: Send + Sync> From<ResourceOwn<T>> for Vec<u8> {
fn from(value: ResourceOwn<T>) -> Self {
value.data
}
}

#[async_trait]
impl<T: Send + Sync> Encode for ResourceOwn<T> {
async fn encode(
self,
payload: &mut (impl bytes::BufMut + Send),
) -> anyhow::Result<Option<AsyncValue>> {
self.data.encode(payload).await
}
}

#[async_trait]
impl<T: Send + Sync> Encode for &ResourceOwn<T> {
async fn encode(
self,
payload: &mut (impl bytes::BufMut + Send),
) -> anyhow::Result<Option<AsyncValue>> {
self.data.as_slice().encode(payload).await
}
}

#[async_trait]
impl<'a, T: Send + Sync> Receive<'a> for ResourceOwn<T> {
#[instrument(level = "trace", skip_all, fields(ty = "resource"))]
async fn receive<S>(
payload: impl Buf + Send + 'a,
rx: &mut (impl Stream<Item = anyhow::Result<Bytes>> + Send + Sync + Unpin),
sub: Option<AsyncSubscription<S>>,
) -> anyhow::Result<(Self, Box<dyn Buf + Send + 'a>)>
where
S: Stream<Item = anyhow::Result<Bytes>> + Send + Sync + 'static,
{
let (subject, payload): (Vec<u8>, _) = Receive::receive(payload, rx, sub).await?;
Ok((subject.into(), payload))
}
}

impl<T: Send + Sync> Subscribe for ResourceOwn<T> {}

#[derive(Debug)]
pub struct ResourceBorrow<T: Send + Sync> {
data: Vec<u8>,
_ty: PhantomData<T>,
}

impl<T: Send + Sync> AsRef<[u8]> for ResourceBorrow<T> {
fn as_ref(&self) -> &[u8] {
&self.data
}
}

impl<T: Send + Sync> From<Vec<u8>> for ResourceBorrow<T> {
fn from(value: Vec<u8>) -> Self {
Self {
data: value,
_ty: PhantomData,
}
}
}

impl<T: Send + Sync> From<ResourceBorrow<T>> for Vec<u8> {
fn from(value: ResourceBorrow<T>) -> Self {
value.data
}
}

#[async_trait]
impl<T: Send + Sync> Encode for ResourceBorrow<T> {
async fn encode(
self,
payload: &mut (impl bytes::BufMut + Send),
) -> anyhow::Result<Option<AsyncValue>> {
self.data.encode(payload).await
}
}

#[async_trait]
impl<T: Send + Sync> Encode for &ResourceBorrow<T> {
async fn encode(
self,
payload: &mut (impl bytes::BufMut + Send),
) -> anyhow::Result<Option<AsyncValue>> {
self.data.as_slice().encode(payload).await
}
}

#[async_trait]
impl<'a, T: Send + Sync> Receive<'a> for ResourceBorrow<T> {
#[instrument(level = "trace", skip_all, fields(ty = "resource"))]
async fn receive<S>(
payload: impl Buf + Send + 'a,
rx: &mut (impl Stream<Item = anyhow::Result<Bytes>> + Send + Sync + Unpin),
sub: Option<AsyncSubscription<S>>,
) -> anyhow::Result<(Self, Box<dyn Buf + Send + 'a>)>
where
S: Stream<Item = anyhow::Result<Bytes>> + Send + Sync + 'static,
{
let (subject, payload): (Vec<u8>, _) = Receive::receive(payload, rx, sub).await?;
Ok((subject.into(), payload))
}
}

impl<T: Send + Sync> Subscribe for ResourceBorrow<T> {}

pub trait Acceptor {
type Subject;
type Transmitter: Transmitter<Subject = Self::Subject> + Send + Sync + 'static;
Expand Down
Loading