Skip to content

Commit

Permalink
Merge pull request #83 from c410-f3r/misc
Browse files Browse the repository at this point in the history
Trim AsyncBounds
  • Loading branch information
c410-f3r authored Jan 14, 2024
2 parents 6e3223e + 609c59e commit be174c5
Show file tree
Hide file tree
Showing 30 changed files with 396 additions and 345 deletions.
7 changes: 3 additions & 4 deletions wtx/examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wtx::{
misc::{AsyncBounds, Stream},
misc::Stream,
rng::StaticRng,
web_socket::{
compression::NegotiatedCompression,
Expand All @@ -14,9 +14,8 @@ pub(crate) async fn _accept_conn_and_echo_frames<C, S>(
stream: S,
) -> wtx::Result<()>
where
C: AsyncBounds + Compression<false>,
C::NegotiatedCompression: AsyncBounds,
S: AsyncBounds + Stream,
C: Compression<false>,
S: Stream,
{
let mut ws = WebSocketAcceptRaw {
compression,
Expand Down
2 changes: 1 addition & 1 deletion wtx/src/client_api_framework/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{fmt::Display, future::Future};

/// Api definitions group different packages into a common namespace and define custom additional
/// logical through hooks.
pub trait Api: AsyncBounds {
pub trait Api {
/// Any custom error structure that can be constructed from [crate::Error].
type Error: Display + From<crate::Error>;

Expand Down
13 changes: 5 additions & 8 deletions wtx/src/client_api_framework/dnsn/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
client_api_framework::{
dnsn::{Deserialize, Serialize},
pkg::Package,
},
misc::AsyncBounds,
use crate::client_api_framework::{
dnsn::{Deserialize, Serialize},
pkg::Package,
};
use alloc::string::String;
use core::marker::PhantomData;
Expand All @@ -21,8 +18,8 @@ impl<EREQC, ERESC> FooBar<EREQC, ERESC> {

impl<DRSR, EREQC, ERESC> Package<(), DRSR, ()> for FooBar<EREQC, ERESC>
where
EREQC: AsyncBounds + Serialize<DRSR>,
ERESC: AsyncBounds + Deserialize<DRSR>,
EREQC: Serialize<DRSR>,
ERESC: Deserialize<DRSR>,
{
type ExternalRequestContent = EREQC;
type ExternalResponseContent = ERESC;
Expand Down
56 changes: 20 additions & 36 deletions wtx/src/client_api_framework/network/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ mod unit;
#[cfg(feature = "web-socket")]
mod wtx;

use crate::{
client_api_framework::{
dnsn::{Deserialize, Serialize},
misc::log_res,
network::TransportGroup,
pkg::{BatchElems, BatchPkg, Package, PkgsAux},
Api, Id,
},
misc::AsyncBounds,
use crate::client_api_framework::{
dnsn::{Deserialize, Serialize},
misc::log_res,
network::TransportGroup,
pkg::{BatchElems, BatchPkg, Package, PkgsAux},
Api, Id,
};
pub use bi_transport::*;
use cl_aux::DynContigColl;
Expand All @@ -46,10 +43,10 @@ pub trait Transport<DRSR> {
&mut self,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
) -> impl AsyncBounds + Future<Output = Result<(), A::Error>>
) -> impl Future<Output = Result<(), A::Error>>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, Self::Params>;
P: Package<A, DRSR, Self::Params>;

/// Sends a request and then awaits its counterpart data response.
///
Expand All @@ -58,10 +55,10 @@ pub trait Transport<DRSR> {
&mut self,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
) -> impl AsyncBounds + Future<Output = Result<Range<usize>, A::Error>>
) -> impl Future<Output = Result<Range<usize>, A::Error>>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, Self::Params>;
P: Package<A, DRSR, Self::Params>;

/// Convenient method similar to [Self::send_retrieve_and_decode_contained] but used for batch
/// requests.
Expand All @@ -73,18 +70,13 @@ pub trait Transport<DRSR> {
pkgs: &mut [P],
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
ress: &mut RESS,
) -> impl AsyncBounds + Future<Output = Result<(), A::Error>>
) -> impl Future<Output = Result<(), A::Error>>
where
A: Api,
DRSR: AsyncBounds,
P: AsyncBounds + Package<A, DRSR, Self::Params>,
P::ExternalRequestContent: AsyncBounds + Borrow<Id> + Ord,
P::ExternalResponseContent: AsyncBounds + Borrow<Id> + Ord,
RESS: AsyncBounds + DynContigColl<P::ExternalResponseContent>,
Self: AsyncBounds,
Self::Params: AsyncBounds,
<Self::Params as TransportParams>::ExternalRequestParams: AsyncBounds,
<Self::Params as TransportParams>::ExternalResponseParams: AsyncBounds,
P: Package<A, DRSR, Self::Params>,
P::ExternalRequestContent: Borrow<Id> + Ord,
P::ExternalResponseContent: Borrow<Id> + Ord,
RESS: DynContigColl<P::ExternalResponseContent>,
for<'any> BatchElems<'any, A, DRSR, P, Self::Params>: Serialize<DRSR>,
{
async {
Expand All @@ -107,15 +99,10 @@ pub trait Transport<DRSR> {
&mut self,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
) -> impl AsyncBounds + Future<Output = Result<P::ExternalResponseContent, A::Error>>
) -> impl Future<Output = Result<P::ExternalResponseContent, A::Error>>
where
A: Api,
DRSR: AsyncBounds,
P: AsyncBounds + Package<A, DRSR, Self::Params>,
Self: AsyncBounds,
Self::Params: AsyncBounds,
<Self::Params as TransportParams>::ExternalRequestParams: AsyncBounds,
<Self::Params as TransportParams>::ExternalResponseParams: AsyncBounds,
P: Package<A, DRSR, Self::Params>,
{
async {
let range = self.send_and_retrieve(pkg, pkgs_aux).await?;
Expand All @@ -136,10 +123,7 @@ pub trait Transport<DRSR> {

impl<DRSR, T> Transport<DRSR> for &mut T
where
DRSR: AsyncBounds,
T: AsyncBounds + Transport<DRSR>,
T::Params: AsyncBounds,
Self: AsyncBounds,
T: Transport<DRSR>,
{
const GROUP: TransportGroup = T::GROUP;
type Params = T::Params;
Expand All @@ -152,7 +136,7 @@ where
) -> Result<(), A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, Self::Params>,
P: Package<A, DRSR, Self::Params>,
{
(**self).send(pkg, pkgs_aux).await
}
Expand All @@ -165,7 +149,7 @@ where
) -> Result<Range<usize>, A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, Self::Params>,
P: Package<A, DRSR, Self::Params>,
{
(**self).send_and_retrieve(pkg, pkgs_aux).await
}
Expand Down
30 changes: 10 additions & 20 deletions wtx/src/client_api_framework/network/transport/bi_transport.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::{
client_api_framework::{
dnsn::Deserialize,
misc::log_res,
network::transport::Transport,
pkg::{Package, PkgsAux},
Api,
},
misc::AsyncBounds,
use crate::client_api_framework::{
dnsn::Deserialize,
misc::log_res,
network::transport::Transport,
pkg::{Package, PkgsAux},
Api,
};
use core::{future::Future, ops::Range};

Expand All @@ -23,24 +20,20 @@ pub trait BiTransport<DRSR>: Transport<DRSR> {
fn retrieve<A>(
&mut self,
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
) -> impl AsyncBounds + Future<Output = crate::Result<Range<usize>>>
) -> impl Future<Output = crate::Result<Range<usize>>>
where
A: Api,
A: AsyncBounds;
A: Api;

/// Internally calls [Self::retrieve] and then tries to decode the defined response specified
/// in [Package::ExternalResponseContent].
#[inline]
fn retrieve_and_decode_contained<A, P>(
&mut self,
pkgs_aux: &mut PkgsAux<A, DRSR, Self::Params>,
) -> impl AsyncBounds + Future<Output = Result<P::ExternalResponseContent, A::Error>>
) -> impl Future<Output = Result<P::ExternalResponseContent, A::Error>>
where
A: Api,
DRSR: AsyncBounds,
P: Package<A, DRSR, Self::Params>,
Self: AsyncBounds,
Self::Params: AsyncBounds,
{
async {
let range = self.retrieve(pkgs_aux).await?;
Expand All @@ -57,10 +50,7 @@ pub trait BiTransport<DRSR>: Transport<DRSR> {

impl<DRSR, T> BiTransport<DRSR> for &mut T
where
DRSR: AsyncBounds,
T: AsyncBounds + BiTransport<DRSR>,
T::Params: AsyncBounds,
Self: AsyncBounds,
T: BiTransport<DRSR>,
{
#[inline]
async fn retrieve<A>(
Expand Down
24 changes: 9 additions & 15 deletions wtx/src/client_api_framework/network/transport/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
clippy::indexing_slicing
)]

use crate::{
client_api_framework::{
misc::{manage_after_sending_related, manage_before_sending_related, FromBytes},
network::{transport::Transport, TransportGroup},
pkg::{Package, PkgsAux},
Api,
},
misc::AsyncBounds,
use crate::client_api_framework::{
misc::{manage_after_sending_related, manage_before_sending_related, FromBytes},
network::{transport::Transport, TransportGroup},
pkg::{Package, PkgsAux},
Api,
};
use alloc::{
borrow::{Cow, ToOwned},
Expand Down Expand Up @@ -91,11 +88,8 @@ where

impl<DRSR, T> Transport<DRSR> for Mock<T>
where
DRSR: AsyncBounds,
T: AsyncBounds + AsRef<[u8]> + Debug + PartialEq + ToOwned + 'static + ?Sized,
<T as ToOwned>::Owned: AsyncBounds + Debug + FromBytes,
for<'owned> &'owned Self: AsyncBounds,
for<'ty> &'ty T: AsyncBounds,
T: AsRef<[u8]> + Debug + PartialEq + ToOwned + 'static + ?Sized,
<T as ToOwned>::Owned: Debug + FromBytes,
{
const GROUP: TransportGroup = TransportGroup::Stub;
type Params = ();
Expand All @@ -108,7 +102,7 @@ where
) -> Result<(), A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, ()>,
P: Package<A, DRSR, ()>,
{
manage_before_sending_related(pkg, pkgs_aux, &mut *self).await?;
self.requests.push(Cow::Owned(FromBytes::from_bytes(&pkgs_aux.byte_buffer)?));
Expand All @@ -125,7 +119,7 @@ where
) -> Result<Range<usize>, A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, ()>,
P: Package<A, DRSR, ()>,
{
<Self as Transport<DRSR>>::send(self, pkg, pkgs_aux).await?;
let response = self.pop_response()?;
Expand Down
12 changes: 4 additions & 8 deletions wtx/src/client_api_framework/network/transport/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
pkg::{Package, PkgsAux},
Api,
},
misc::{from_utf8_basic_rslt, AsyncBounds},
misc::from_utf8_basic_rslt,
};
use core::ops::Range;
use reqwest::{
Expand All @@ -30,10 +30,7 @@ use reqwest::{
/// .await?;
/// # Ok(()) }
/// ```
impl<DRSR> Transport<DRSR> for Client
where
DRSR: AsyncBounds,
{
impl<DRSR> Transport<DRSR> for Client {
const GROUP: TransportGroup = TransportGroup::HTTP;
type Params = HttpParams;

Expand All @@ -45,7 +42,7 @@ where
) -> Result<(), A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, HttpParams>,
P: Package<A, DRSR, HttpParams>,
{
let _res = response(self, pkg, pkgs_aux).await?;
Ok(())
Expand All @@ -59,7 +56,7 @@ where
) -> Result<Range<usize>, A::Error>
where
A: Api,
P: AsyncBounds + Package<A, DRSR, HttpParams>,
P: Package<A, DRSR, HttpParams>,
{
let res = response(self, pkg, pkgs_aux).await?;
let received_bytes = res.bytes().await.map_err(Into::into)?;
Expand All @@ -75,7 +72,6 @@ async fn response<A, DRSR, P>(
) -> Result<reqwest::Response, A::Error>
where
A: Api,
DRSR: AsyncBounds,
P: Package<A, DRSR, HttpParams>,
{
fn manage_data<A, DRSR>(
Expand Down
Loading

0 comments on commit be174c5

Please sign in to comment.