mini-serve 0.6.0

An HTTP server: trie router, middleware, CORS, optional TLS. Built on hyper + tokio.
Documentation
use std::ops::Deref;
use std::sync::Arc;

/// Shared application state passed to every request handler.
///
/// Wraps an `Arc<S>` for zero-allocation sharing across handlers. Handlers
/// receive a clone of this wrapper (not a clone of `S`), which only increments
/// the refcount. Dereferences transparently to `&S`.
#[derive(Clone)]
pub struct State<S>(Arc<S>);

impl<S> State<S> {
	/// Create a new state from a value, wrapping it in an `Arc`.
	pub fn new(state: S) -> Self {
		State(Arc::new(state))
	}

	/// Create a state from an existing `Arc`.
	///
	/// Useful for sharing state between the app and background tasks without
	/// allocating a second `Arc`.
	pub fn from_arc(state: Arc<S>) -> Self {
		State(state)
	}

	/// Extract a clone of the inner value.
	///
	/// Only available if `S` implements `Clone`. This is generally not needed
	/// in request handlers (dereference to `&S` instead); it's useful for
	/// copying state into spawned background tasks.
	pub fn inner(&self) -> S
	where
		S: Clone,
	{
		S::clone(&self.0)
	}
}

impl<S> Deref for State<S> {
	type Target = S;

	fn deref(&self) -> &S {
		&self.0
	}
}