no-std compatiblity for underlying traits (#810)

This commit is contained in:
Tait Hoyem 2025-05-31 16:02:07 -06:00 committed by GitHub
parent 81658e65ad
commit ec81e5797b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 38 additions and 13 deletions

View File

@ -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

View File

@ -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
[Tower]: https://crates.io/crates/tower

View File

@ -1,5 +1,5 @@
use super::Layer;
use std::fmt;
use core::fmt;
/// A no-op middleware.
///

View File

@ -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<S> {
@ -88,7 +88,7 @@ where
impl<F> fmt::Debug for LayerFn<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LayerFn")
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.field("f", &format_args!("{}", core::any::type_name::<F>()))
.finish()
}
}
@ -96,6 +96,7 @@ impl<F> fmt::Debug for LayerFn<F> {
#[cfg(test)]
mod tests {
use super::*;
use alloc::{format, string::ToString};
#[allow(dead_code)]
#[test]

View File

@ -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,

View File

@ -1,5 +1,5 @@
use super::Layer;
use std::fmt;
use core::fmt;
/// Two middlewares chained together.
#[derive(Clone)]

View File

@ -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).

View File

@ -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;