From ec81e5797bb80ea7b65d243312db6ec2fdb6ce0e Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Sat, 31 May 2025 16:02:07 -0600 Subject: [PATCH] `no-std` compatiblity for underlying traits (#810) --- README.md | 4 ++++ tower-layer/README.md | 4 +++- tower-layer/src/identity.rs | 2 +- tower-layer/src/layer_fn.rs | 11 ++++++----- tower-layer/src/lib.rs | 9 +++++++-- tower-layer/src/stack.rs | 2 +- tower-service/README.md | 5 +++++ tower-service/src/lib.rs | 14 +++++++++++--- 8 files changed, 38 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c250b1ce..1a5d51e3 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ Tower will keep a rolling MSRV (minimum supported Rust version) policy of **at least** 6 months. When increasing the MSRV, the new Rust version must have been released at least six months ago. The current MSRV is 1.64.0. +## `no_std` + +`tower` itself is _not_ `no_std` compatible, but `tower-layer` and `tower-service` are. + ## Getting Started If you're brand new to Tower and want to start with the basics we recommend you diff --git a/tower-layer/README.md b/tower-layer/README.md index 48f9dbb7..b456119d 100644 --- a/tower-layer/README.md +++ b/tower-layer/README.md @@ -30,6 +30,8 @@ reusable components that can be applied to very different kinds of services; for example, it can be applied to services operating on different protocols, and to both the client and server side of a network transaction. +`tower-layer` is `no_std` compatible. + ## License This project is licensed under the [MIT license](LICENSE). @@ -40,4 +42,4 @@ Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tower by you, shall be licensed as MIT, without any additional terms or conditions. -[Tower]: https://crates.io/crates/tower \ No newline at end of file +[Tower]: https://crates.io/crates/tower diff --git a/tower-layer/src/identity.rs b/tower-layer/src/identity.rs index 5233a1d8..a5192c51 100644 --- a/tower-layer/src/identity.rs +++ b/tower-layer/src/identity.rs @@ -1,5 +1,5 @@ use super::Layer; -use std::fmt; +use core::fmt; /// A no-op middleware. /// diff --git a/tower-layer/src/layer_fn.rs b/tower-layer/src/layer_fn.rs index 06f6e0e3..e00f3373 100644 --- a/tower-layer/src/layer_fn.rs +++ b/tower-layer/src/layer_fn.rs @@ -1,5 +1,5 @@ use super::Layer; -use std::fmt; +use core::fmt; /// Returns a new [`LayerFn`] that implements [`Layer`] by calling the /// given function. @@ -13,10 +13,10 @@ use std::fmt; /// # Example /// ```rust /// # use tower::Service; -/// # use std::task::{Poll, Context}; +/// # use core::task::{Poll, Context}; /// # use tower_layer::{Layer, layer_fn}; -/// # use std::fmt; -/// # use std::convert::Infallible; +/// # use core::fmt; +/// # use core::convert::Infallible; /// # /// // A middleware that logs requests before forwarding them to another service /// pub struct LogService { @@ -88,7 +88,7 @@ where impl fmt::Debug for LayerFn { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("LayerFn") - .field("f", &format_args!("{}", std::any::type_name::())) + .field("f", &format_args!("{}", core::any::type_name::())) .finish() } } @@ -96,6 +96,7 @@ impl fmt::Debug for LayerFn { #[cfg(test)] mod tests { use super::*; + use alloc::{format, string::ToString}; #[allow(dead_code)] #[test] diff --git a/tower-layer/src/lib.rs b/tower-layer/src/lib.rs index 4823fcc1..2ed6ac56 100644 --- a/tower-layer/src/lib.rs +++ b/tower-layer/src/lib.rs @@ -16,6 +16,11 @@ //! //! [`Service`]: https://docs.rs/tower/*/tower/trait.Service.html +#![no_std] + +#[cfg(test)] +extern crate alloc; + mod identity; mod layer_fn; mod stack; @@ -41,9 +46,9 @@ pub use self::{ /// /// ```rust /// # use tower_service::Service; -/// # use std::task::{Poll, Context}; +/// # use core::task::{Poll, Context}; /// # use tower_layer::Layer; -/// # use std::fmt; +/// # use core::fmt; /// /// pub struct LogLayer { /// target: &'static str, diff --git a/tower-layer/src/stack.rs b/tower-layer/src/stack.rs index cb6bac7b..e6ff0d1a 100644 --- a/tower-layer/src/stack.rs +++ b/tower-layer/src/stack.rs @@ -1,5 +1,5 @@ use super::Layer; -use std::fmt; +use core::fmt; /// Two middlewares chained together. #[derive(Clone)] diff --git a/tower-service/README.md b/tower-service/README.md index 3f2766db..49908f17 100644 --- a/tower-service/README.md +++ b/tower-service/README.md @@ -45,6 +45,11 @@ middleware may take actions such as modify the request. [`Service`]: https://docs.rs/tower-service/latest/tower_service/trait.Service.html [Tower]: https://crates.io/crates/tower + +## `no_std` + +`tower-service` is `no_std` compatible, but it (currently) requires the `alloc` crate. + ## License This project is licensed under the [MIT license](LICENSE). diff --git a/tower-service/src/lib.rs b/tower-service/src/lib.rs index 58cc2eb1..4c75307b 100644 --- a/tower-service/src/lib.rs +++ b/tower-service/src/lib.rs @@ -13,8 +13,16 @@ //! request / response clients and servers. It is simple but powerful and is //! used as the foundation for the rest of Tower. -use std::future::Future; -use std::task::{Context, Poll}; +#![no_std] + +extern crate alloc; + +use alloc::boxed::Box; + +use core::future::Future; +use core::marker::Sized; +use core::result::Result; +use core::task::{Context, Poll}; /// An asynchronous function from a `Request` to a `Response`. /// @@ -273,7 +281,7 @@ use std::task::{Context, Poll}; /// } /// ``` /// -/// You should instead use [`std::mem::replace`] to take the service that was ready: +/// You should instead use [`core::mem::replace`] to take the service that was ready: /// /// ```rust /// # use std::pin::Pin;