Skip to content

Commit ec81e57

Browse files
authored
no-std compatiblity for underlying traits (#810)
1 parent 81658e6 commit ec81e57

File tree

8 files changed

+38
-13
lines changed

8 files changed

+38
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Tower will keep a rolling MSRV (minimum supported Rust version) policy of **at
3535
least** 6 months. When increasing the MSRV, the new Rust version must have been
3636
released at least six months ago. The current MSRV is 1.64.0.
3737

38+
## `no_std`
39+
40+
`tower` itself is _not_ `no_std` compatible, but `tower-layer` and `tower-service` are.
41+
3842
## Getting Started
3943

4044
If you're brand new to Tower and want to start with the basics we recommend you

tower-layer/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ reusable components that can be applied to very different kinds of services;
3030
for example, it can be applied to services operating on different protocols,
3131
and to both the client and server side of a network transaction.
3232

33+
`tower-layer` is `no_std` compatible.
34+
3335
## License
3436

3537
This project is licensed under the [MIT license](LICENSE).
@@ -40,4 +42,4 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
4042
for inclusion in Tower by you, shall be licensed as MIT, without any additional
4143
terms or conditions.
4244

43-
[Tower]: https://crates.io/crates/tower
45+
[Tower]: https://crates.io/crates/tower

tower-layer/src/identity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::Layer;
2-
use std::fmt;
2+
use core::fmt;
33

44
/// A no-op middleware.
55
///

tower-layer/src/layer_fn.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::Layer;
2-
use std::fmt;
2+
use core::fmt;
33

44
/// Returns a new [`LayerFn`] that implements [`Layer`] by calling the
55
/// given function.
@@ -13,10 +13,10 @@ use std::fmt;
1313
/// # Example
1414
/// ```rust
1515
/// # use tower::Service;
16-
/// # use std::task::{Poll, Context};
16+
/// # use core::task::{Poll, Context};
1717
/// # use tower_layer::{Layer, layer_fn};
18-
/// # use std::fmt;
19-
/// # use std::convert::Infallible;
18+
/// # use core::fmt;
19+
/// # use core::convert::Infallible;
2020
/// #
2121
/// // A middleware that logs requests before forwarding them to another service
2222
/// pub struct LogService<S> {
@@ -88,14 +88,15 @@ where
8888
impl<F> fmt::Debug for LayerFn<F> {
8989
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9090
f.debug_struct("LayerFn")
91-
.field("f", &format_args!("{}", std::any::type_name::<F>()))
91+
.field("f", &format_args!("{}", core::any::type_name::<F>()))
9292
.finish()
9393
}
9494
}
9595

9696
#[cfg(test)]
9797
mod tests {
9898
use super::*;
99+
use alloc::{format, string::ToString};
99100

100101
#[allow(dead_code)]
101102
#[test]

tower-layer/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
//!
1717
//! [`Service`]: https://docs.rs/tower/*/tower/trait.Service.html
1818
19+
#![no_std]
20+
21+
#[cfg(test)]
22+
extern crate alloc;
23+
1924
mod identity;
2025
mod layer_fn;
2126
mod stack;
@@ -41,9 +46,9 @@ pub use self::{
4146
///
4247
/// ```rust
4348
/// # use tower_service::Service;
44-
/// # use std::task::{Poll, Context};
49+
/// # use core::task::{Poll, Context};
4550
/// # use tower_layer::Layer;
46-
/// # use std::fmt;
51+
/// # use core::fmt;
4752
///
4853
/// pub struct LogLayer {
4954
/// target: &'static str,

tower-layer/src/stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::Layer;
2-
use std::fmt;
2+
use core::fmt;
33

44
/// Two middlewares chained together.
55
#[derive(Clone)]

tower-service/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ middleware may take actions such as modify the request.
4545

4646
[`Service`]: https://docs.rs/tower-service/latest/tower_service/trait.Service.html
4747
[Tower]: https://crates.io/crates/tower
48+
49+
## `no_std`
50+
51+
`tower-service` is `no_std` compatible, but it (currently) requires the `alloc` crate.
52+
4853
## License
4954

5055
This project is licensed under the [MIT license](LICENSE).

tower-service/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
//! request / response clients and servers. It is simple but powerful and is
1414
//! used as the foundation for the rest of Tower.
1515
16-
use std::future::Future;
17-
use std::task::{Context, Poll};
16+
#![no_std]
17+
18+
extern crate alloc;
19+
20+
use alloc::boxed::Box;
21+
22+
use core::future::Future;
23+
use core::marker::Sized;
24+
use core::result::Result;
25+
use core::task::{Context, Poll};
1826

1927
/// An asynchronous function from a `Request` to a `Response`.
2028
///
@@ -273,7 +281,7 @@ use std::task::{Context, Poll};
273281
/// }
274282
/// ```
275283
///
276-
/// You should instead use [`std::mem::replace`] to take the service that was ready:
284+
/// You should instead use [`core::mem::replace`] to take the service that was ready:
277285
///
278286
/// ```rust
279287
/// # use std::pin::Pin;

0 commit comments

Comments
 (0)