diff --git a/src/extractor.rs b/src/extractor.rs index 18961d89..47473150 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -37,6 +37,16 @@ pub struct SharedState(Arc); impl SharedState { /// Construct a new [`SharedState`]. #[must_use] + /// Creates a new `SharedState` instance wrapping the provided `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// let state = Arc::new(42); + /// let shared = SharedState::new(state.clone()); + /// assert_eq!(*shared, 42); + /// ``` pub fn new(inner: Arc) -> Self { Self(inner) } @@ -45,6 +55,18 @@ impl SharedState { impl std::ops::Deref for SharedState { type Target = T; + /// Returns a reference to the inner shared state value. + /// + /// This allows transparent access to the underlying state managed by `SharedState`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// let state = Arc::new(42); + /// let shared = SharedState::new(state.clone()); + /// assert_eq!(*shared, 42); + /// ``` fn deref(&self) -> &Self::Target { &self.0 } diff --git a/src/middleware.rs b/src/middleware.rs index c4ae3b46..bdaa1e21 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -20,7 +20,14 @@ impl<'a, S> Next<'a, S> where S: Service + ?Sized, { - /// Construct a new [`Next`] wrapper. + /// Creates a new `Next` instance wrapping a reference to the given service. + /// + /// # Examples + /// + /// ``` + /// let service = MyService::default(); + /// let next = Next::new(&service); + /// ``` pub const fn new(service: &'a S) -> Self { Self { service } } @@ -29,7 +36,28 @@ where /// /// # Errors /// - /// Propagates any error returned by the wrapped service. + /// Asynchronously invokes the next service in the middleware chain with the given request. + /// + /// Returns the response from the wrapped service, or propagates any error produced. + /// + /// # Examples + /// + /// ``` + /// # use your_crate::{ServiceRequest, ServiceResponse, Next, Service}; + /// # struct DummyService; + /// # #[async_trait::async_trait] + /// # impl Service for DummyService { + /// # type Error = std::convert::Infallible; + /// # async fn call(&self, _req: ServiceRequest) -> Result { + /// # Ok(ServiceResponse::default()) + /// # } + /// # } + /// # let service = DummyService; + /// let next = Next::new(&service); + /// let req = ServiceRequest {}; + /// let res = tokio_test::block_on(next.call(req)); + /// assert!(res.is_ok()); + /// ``` pub async fn call(&self, req: ServiceRequest) -> Result { self.service.call(req).await } diff --git a/src/server.rs b/src/server.rs index a58e97f4..0e2200c7 100644 --- a/src/server.rs +++ b/src/server.rs @@ -36,6 +36,14 @@ where /// /// ```ignore /// let server = WireframeServer::new(|| WireframeApp::default()); + /// Creates a new `WireframeServer` with the given factory closure. + /// + /// The server is initialised with the default number of worker tasks equal to the number of CPU cores. The TCP listener is not yet bound; use `bind` to associate a socket address. + /// + /// # Examples + /// + /// ```ignore + /// let server = WireframeServer::new(|| WireframeApp::default()); /// ``` pub fn new(factory: F) -> Self { Self { @@ -61,6 +69,12 @@ where /// /// ```ignore /// let server = WireframeServer::new(factory).workers(4); + /// Sets the number of worker tasks for the server, ensuring at least one worker. + /// + /// # Examples + /// + /// ```ignore + /// let server = WireframeServer::new(factory).workers(4); /// ``` pub fn workers(mut self, count: usize) -> Self { self.workers = count.max(1);