use alloc::{borrow::Cow, string::String, vec::Vec};
use core::{future::Future, ops, str, time::Duration};
use futures_util::future;
pub mod async_std;
pub trait PlatformRef: Clone + Send + Sync + 'static {
type Delay: Future<Output = ()> + Unpin + Send + 'static;
type Yield: Future<Output = ()> + Unpin + Send + 'static;
type Instant: Clone
+ ops::Add<Duration, Output = Self::Instant>
+ ops::Sub<Self::Instant, Output = Duration>
+ PartialOrd
+ Ord
+ PartialEq
+ Eq
+ Send
+ Sync
+ 'static;
type Connection: Send + Sync + 'static;
type Stream: Send + 'static;
type ConnectFuture: Future<Output = Result<PlatformConnection<Self::Stream, Self::Connection>, ConnectError>>
+ Unpin
+ Send
+ 'static;
type StreamUpdateFuture<'a>: Future<Output = ()> + Unpin + Send + 'a;
type NextSubstreamFuture<'a>: Future<Output = Option<(Self::Stream, PlatformSubstreamDirection)>>
+ Unpin
+ Send
+ 'a;
fn now_from_unix_epoch(&self) -> Duration;
fn now(&self) -> Self::Instant;
fn sleep(&self, duration: Duration) -> Self::Delay;
fn sleep_until(&self, when: Self::Instant) -> Self::Delay;
fn spawn_task(&self, task_name: Cow<str>, task: future::BoxFuture<'static, ()>);
fn client_name(&self) -> Cow<str>;
fn client_version(&self) -> Cow<str>;
fn yield_after_cpu_intensive(&self) -> Self::Yield;
fn connect(&self, url: &str) -> Self::ConnectFuture;
fn open_out_substream(&self, connection: &mut Self::Connection);
fn next_substream<'a>(
&self,
connection: &'a mut Self::Connection,
) -> Self::NextSubstreamFuture<'a>;
fn update_stream<'a>(&self, stream: &'a mut Self::Stream) -> Self::StreamUpdateFuture<'a>;
fn read_buffer<'a>(&self, stream: &'a mut Self::Stream) -> ReadBuffer<'a>;
fn advance_read_cursor(&self, stream: &mut Self::Stream, bytes: usize);
fn writable_bytes(&self, stream: &mut Self::Stream) -> usize;
fn send(&self, stream: &mut Self::Stream, data: &[u8]);
fn close_send(&self, stream: &mut Self::Stream);
}
#[derive(Debug)]
pub enum PlatformConnection<TStream, TConnection> {
SingleStreamMultistreamSelectNoiseYamux(TStream),
MultiStreamWebRtc {
connection: TConnection,
local_tls_certificate_multihash: Vec<u8>,
remote_tls_certificate_multihash: Vec<u8>,
},
}
#[derive(Debug)]
pub enum PlatformSubstreamDirection {
Inbound,
Outbound,
}
pub struct ConnectError {
pub message: String,
pub is_bad_addr: bool,
}
#[derive(Debug)]
pub enum ReadBuffer<'a> {
Open(&'a [u8]),
Closed,
Reset,
}