Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ pub struct SharedState<T>(Arc<T>);
impl<T> SharedState<T> {
/// Construct a new [`SharedState`].
#[must_use]
/// Creates a new `SharedState` instance wrapping the provided `Arc<T>`.
///
/// # 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<T>) -> Self {
Self(inner)
}
Expand All @@ -45,6 +55,18 @@ impl<T> SharedState<T> {
impl<T> std::ops::Deref for SharedState<T> {
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
}
Expand Down
32 changes: 30 additions & 2 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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<ServiceResponse, Self::Error> {
/// # 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<ServiceResponse, S::Error> {
self.service.call(req).await
}
Expand Down
14 changes: 14 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down