Skip to content

Commit

Permalink
Basic documentation to the xwt-web-sys and xwt-wtransport (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII authored Feb 3, 2024
1 parent 8424c45 commit 66e93a5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/xwt-web-sys/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Error type definition.
/// A generic error encapsulating a JS-land value.
#[derive(Debug)]
pub struct Error(pub wasm_bindgen::JsValue);

Expand Down
23 changes: 22 additions & 1 deletion crates/xwt-web-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
doc = "The `web_sys`-powered implementation of `xwt_core`."
)]
#![cfg(target_family = "wasm")]
#![allow(missing_docs, clippy::missing_docs_in_private_items)]

use std::rc::Rc;

Expand All @@ -17,8 +16,13 @@ mod error;

pub use error::*;

/// An endpoint for the xwt.
///
/// Internally holds the connection options and can create
/// a new `WebTransport` object on the "web" side on a connection request.
#[derive(Debug, Clone, Default)]
pub struct Endpoint {
/// The options to use to create the `WebTransport`s.
pub options: web_sys::WebTransportOptions,
}

Expand Down Expand Up @@ -46,10 +50,16 @@ impl xwt_core::traits::EndpointConnect for Endpoint {
}
}

/// Connection hold the [`web_sys::WebTransport`] and is responsible for
/// providing access to the Web API of WebTransport in a way that is portable.
/// It also holds handles to the datagram reader and writer.
#[derive(Debug)]
pub struct Connection {
/// The WebTransport instance.
pub transport: Rc<web_sys::WebTransport>,
/// The datagram reader.
pub datagram_readable_stream_reader: web_sys::ReadableStreamDefaultReader,
/// The datagram writer.
pub datagram_writable_stream_writer: web_sys::WritableStreamDefaultWriter,
}

Expand All @@ -58,18 +68,27 @@ impl xwt_core::traits::Streams for Connection {
type RecvStream = RecvStream;
}

/// Send the data into a WebTransport stream.
pub struct SendStream {
/// The WebTransport instance.
pub transport: Rc<web_sys::WebTransport>,
/// The handle to the stream to write to.
pub stream: web_sys::WebTransportSendStream,
/// A writer to conduct the operation.
pub writer: web_sys_async_io::Writer,
}

/// Recv the data from a WebTransport stream.
pub struct RecvStream {
/// The WebTransport instance.
pub transport: Rc<web_sys::WebTransport>,
/// The handle to the stream to read from.
pub stream: web_sys::WebTransportReceiveStream,
/// A reader to conduct the operation.
pub reader: web_sys_async_io::Reader,
}

/// Open a reader for the given stream and create a [`RecvStream`].
fn wrap_recv_stream(
transport: &Rc<web_sys::WebTransport>,
stream: web_sys::WebTransportReceiveStream,
Expand All @@ -86,6 +105,7 @@ fn wrap_recv_stream(
}
}

/// Open a writer for the given stream and create a [`SendStream`].
fn wrap_send_stream(
transport: &Rc<web_sys::WebTransport>,
stream: web_sys::WebTransportSendStream,
Expand All @@ -99,6 +119,7 @@ fn wrap_send_stream(
}
}

/// Take a bidi stream and wrap a reader and writer for it.
fn wrap_bi_stream(
transport: &Rc<web_sys::WebTransport>,
stream: web_sys::WebTransportBidirectionalStream,
Expand Down
2 changes: 1 addition & 1 deletion crates/xwt-wtransport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
doc = "The `wtransport`-powered implementation of `xwt_core`."
)]
#![cfg(not(target_family = "wasm"))]
#![allow(missing_docs, clippy::missing_docs_in_private_items)]

use xwt_core::async_trait;

Expand Down Expand Up @@ -73,6 +72,7 @@ impl xwt_core::traits::Streams for Connection {
type RecvStream = RecvStream;
}

/// Take a pair of stream ends and wrap into our newtypes.
fn map_streams(
streams: (wtransport::SendStream, wtransport::RecvStream),
) -> (SendStream, RecvStream) {
Expand Down
5 changes: 5 additions & 0 deletions crates/xwt-wtransport/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Newtype definitions.
/// Create a newtype wrapper for a given type.
macro_rules! newtype {
($name:ident => $wrapped_type:path) => {
/// A [`$wrapped_type`] newtype.
pub struct $name(pub $wrapped_type);

impl std::fmt::Debug for $name {
Expand All @@ -9,6 +13,7 @@ macro_rules! newtype {
}
};
($name:ident < $($generics:tt),* > => $wrapped_type:path) => {
/// A [`$wrapped_type`] newtype.
pub struct $name<$($generics)*>(pub $wrapped_type);

impl<$($generics)*> std::fmt::Debug for $name<$($generics)*> {
Expand Down
1 change: 0 additions & 1 deletion crates/xwt-wtransport/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(not(target_family = "wasm"))]
#![feature(once_cell_try)]
#![allow(missing_docs, clippy::missing_docs_in_private_items)]

fn setup() -> color_eyre::eyre::Result<()> {
static INIT: std::sync::OnceLock<()> = std::sync::OnceLock::new();
Expand Down

0 comments on commit 66e93a5

Please sign in to comment.