Skip to content

Commit

Permalink
feat: Make new functions const when possible
Browse files Browse the repository at this point in the history
The main reason was to allow to initialize the RateLimitLayer in a const context.
So why not making ever new function const (wherever it's possible). :P
  • Loading branch information
kerkmann committed Jan 7, 2024
1 parent 39adf5c commit bd5db7f
Show file tree
Hide file tree
Showing 37 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion tower/src/balance/p2c/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct MakeBalanceLayer<D, Req> {

impl<D, Req> MakeBalanceLayer<D, Req> {
/// Build balancers using operating system entropy.
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
_marker: PhantomData,
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/balance/p2c/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pin_project! {

impl<S, Req> MakeBalance<S, Req> {
/// Build balancers using operating system entropy.
pub fn new(make_discover: S) -> Self {
pub const fn new(make_discover: S) -> Self {
Self {
inner: make_discover,
_marker: PhantomData,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/buffer/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<Request> BufferLayer<Request> {
/// [`Poll::Ready`]: std::task::Poll::Ready
/// [`call`]: crate::Service::call
/// [`poll_ready`]: crate::Service::poll_ready
pub fn new(bound: usize) -> Self {
pub const fn new(bound: usize) -> Self {
BufferLayer {
bound,
_p: PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions tower/src/filter/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<U> FilterLayer<U> {
///
/// [`Predicate`]: crate::filter::Predicate
/// [`Filter`]: crate::filter::Filter
pub fn new(predicate: U) -> Self {
pub const fn new(predicate: U) -> Self {
Self { predicate }
}
}
Expand All @@ -57,7 +57,7 @@ impl<U> AsyncFilterLayer<U> {
///
/// [`AsyncPredicate`]: crate::filter::AsyncPredicate
/// [`Filter`]: crate::filter::Filter
pub fn new(predicate: U) -> Self {
pub const fn new(predicate: U) -> Self {
Self { predicate }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct AsyncFilter<T, U> {

impl<T, U> Filter<T, U> {
/// Returns a new [`Filter`] service wrapping `inner`.
pub fn new(inner: T, predicate: U) -> Self {
pub const fn new(inner: T, predicate: U) -> Self {
Self { inner, predicate }
}

Expand Down Expand Up @@ -123,7 +123,7 @@ where

impl<T, U> AsyncFilter<T, U> {
/// Returns a new [`AsyncFilter`] service wrapping `inner`.
pub fn new(inner: T, predicate: U) -> Self {
pub const fn new(inner: T, predicate: U) -> Self {
Self { inner, predicate }
}

Expand Down
2 changes: 1 addition & 1 deletion tower/src/hedge/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<Request, F> State<Request, F> {
}

impl<P, S> Delay<P, S> {
pub fn new<Request>(policy: P, service: S) -> Self
pub const fn new<Request>(policy: P, service: S) -> Self
where
P: Policy<Request>,
S: Service<Request> + Clone,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/hedge/latency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<S, R> Latency<R, S>
where
R: Record + Clone,
{
pub fn new<Request>(rec: R, service: S) -> Self
pub const fn new<Request>(rec: R, service: S) -> Self
where
S: Service<Request>,
S::Error: Into<crate::BoxError>,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/hedge/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pin_project! {
}

impl<P, A, B> Select<P, A, B> {
pub fn new<Request>(policy: P, a: A, b: B) -> Self
pub const fn new<Request>(policy: P, a: A, b: B) -> Self
where
P: Policy<Request>,
A: Service<Request>,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/limit/concurrency/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ConcurrencyLimitLayer {

impl ConcurrencyLimitLayer {
/// Create a new concurrency limit layer.
pub fn new(max: usize) -> Self {
pub const fn new(max: usize) -> Self {
ConcurrencyLimitLayer { max }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/limit/rate/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct RateLimitLayer {

impl RateLimitLayer {
/// Create new rate limit layer.
pub fn new(num: u64, per: Duration) -> Self {
pub const fn new(num: u64, per: Duration) -> Self {
let rate = Rate::new(num, per);
RateLimitLayer { rate }
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/limit/rate/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ impl Rate {
/// # Panics
///
/// This function panics if `num` or `per` is 0.
pub fn new(num: u64, per: Duration) -> Self {
pub const fn new(num: u64, per: Duration) -> Self {
assert!(num > 0);
assert!(per > Duration::from_millis(0));
assert!(!per.is_zero());

Rate { num, per }
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pin_project! {

impl<F, C, H> TrackCompletionFuture<F, C, H> {
/// Wraps a future, propagating the tracker into its value if successful.
pub fn new(completion: C, handle: H, future: F) -> Self {
pub const fn new(completion: C, handle: H, future: F) -> Self {
TrackCompletionFuture {
future,
completion,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pin_project! {

impl<T, M: Copy> Constant<T, M> {
/// Wraps a `T`-typed service with a constant `M`-typed load metric.
pub fn new(inner: T, load: M) -> Self {
pub const fn new(inner: T, load: M) -> Self {
Self { inner, load }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load/pending_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
#[cfg(feature = "discover")]
impl<D, C> PendingRequestsDiscover<D, C> {
/// Wraps a [`Discover`], wrapping all of its services with [`PendingRequests`].
pub fn new<Request>(discover: D, completion: C) -> Self
pub const fn new<Request>(discover: D, completion: C) -> Self
where
D: Discover,
D::Service: Service<Request>,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load_shed/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Overloaded {

impl Overloaded {
/// Construct a new overloaded error
pub fn new() -> Self {
pub const fn new() -> Self {
Overloaded { _p: () }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load_shed/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct LoadShedLayer {

impl LoadShedLayer {
/// Creates a new layer.
pub fn new() -> Self {
pub const fn new() -> Self {
LoadShedLayer { _p: () }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/load_shed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct LoadShed<S> {

impl<S> LoadShed<S> {
/// Wraps a service in [`LoadShed`] middleware.
pub fn new(inner: S) -> Self {
pub const fn new(inner: S) -> Self {
LoadShed {
inner,
is_ready: false,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/make/make_service/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Shared<S> {

impl<S> Shared<S> {
/// Create a new [`Shared`] from a service.
pub fn new(service: S) -> Self {
pub const fn new(service: S) -> Self {
Self { service }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/reconnect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
M: Service<Target>,
{
/// Lazily connect and reconnect to a [`Service`].
pub fn new<S, Request>(mk_service: M, target: Target) -> Self {
pub const fn new<S, Request>(mk_service: M, target: Target) -> Self {
Reconnect {
mk_service,
state: State::Idle,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/retry/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct RetryLayer<P> {

impl<P> RetryLayer<P> {
/// Creates a new [`RetryLayer`] from a retry policy.
pub fn new(policy: P) -> Self {
pub const fn new(policy: P) -> Self {
RetryLayer { policy }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/retry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pin_project! {

impl<P, S> Retry<P, S> {
/// Retry the inner service depending on this [`Policy`].
pub fn new(policy: P, service: S) -> Self {
pub const fn new(policy: P, service: S) -> Self {
Retry { policy, service }
}

Expand Down
2 changes: 1 addition & 1 deletion tower/src/spawn_ready/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum Inner<S> {

impl<S> SpawnReady<S> {
/// Creates a new [`SpawnReady`] wrapping `service`.
pub fn new(service: S) -> Self {
pub const fn new(service: S) -> Self {
Self {
inner: Inner::Service(Some(service)),
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/timeout/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Elapsed(pub(super) ());

impl Elapsed {
/// Construct a new elapsed error
pub fn new() -> Self {
pub const fn new() -> Self {
Elapsed(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/timeout/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct TimeoutLayer {

impl TimeoutLayer {
/// Create a timeout from a duration
pub fn new(timeout: Duration) -> Self {
pub const fn new(timeout: Duration) -> Self {
TimeoutLayer { timeout }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/timeout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Timeout<T> {

impl<T> Timeout<T> {
/// Creates a new [`Timeout`]
pub fn new(inner: T, timeout: Duration) -> Self {
pub const fn new(inner: T, timeout: Duration) -> Self {
Timeout { inner, timeout }
}

Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct AndThenLayer<F> {

impl<S, F> AndThen<S, F> {
/// Creates a new `AndThen` service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
AndThen { f, inner }
}

Expand Down Expand Up @@ -110,7 +110,7 @@ where

impl<F> AndThenLayer<F> {
/// Creates a new [`AndThenLayer`] layer.
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
AndThenLayer { f }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower/src/util/call_all/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
S: Stream,
Q: Drive<Svc::Future>,
{
pub(crate) fn new(service: Svc, stream: S, queue: Q) -> CallAll<Svc, S, Q> {
pub(crate) const fn new(service: Svc, stream: S, queue: Q) -> CallAll<Svc, S, Q> {
CallAll {
service: Some(service),
stream,
Expand Down
2 changes: 1 addition & 1 deletion tower/src/util/future_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<F, S> FutureService<F, S> {
///
/// This will most likely come up if you're calling `future_service` with an async block. In that
/// case you can use `Box::pin(async { ... })` as shown in the example.
pub fn new(future: F) -> Self {
pub const fn new(future: F) -> Self {
Self {
state: State::Future(future),
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ opaque_future! {

impl<S, F> MapErr<S, F> {
/// Creates a new [`MapErr`] service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
MapErr { f, inner }
}

Expand Down Expand Up @@ -78,7 +78,7 @@ where

impl<F> MapErrLayer<F> {
/// Creates a new [`MapErrLayer`].
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
MapErrLayer { f }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/map_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct MapFuture<S, F> {

impl<S, F> MapFuture<S, F> {
/// Creates a new [`MapFuture`] service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
Self { inner, f }
}

Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct MapFutureLayer<F> {

impl<F> MapFutureLayer<F> {
/// Creates a new [`MapFutureLayer`] layer.
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
Self { f }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/map_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where

impl<S, F> MapRequest<S, F> {
/// Creates a new [`MapRequest`] service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
MapRequest { inner, f }
}

Expand Down Expand Up @@ -70,7 +70,7 @@ pub struct MapRequestLayer<F> {

impl<F> MapRequestLayer<F> {
/// Creates a new [`MapRequestLayer`].
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
MapRequestLayer { f }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/map_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ opaque_future! {

impl<S, F> MapResponse<S, F> {
/// Creates a new `MapResponse` service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
MapResponse { f, inner }
}

Expand Down Expand Up @@ -78,7 +78,7 @@ where

impl<F> MapResponseLayer<F> {
/// Creates a new [`MapResponseLayer`] layer.
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
MapResponseLayer { f }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower/src/util/map_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ opaque_future! {

impl<S, F> MapResult<S, F> {
/// Creates a new [`MapResult`] service.
pub fn new(inner: S, f: F) -> Self {
pub const fn new(inner: S, f: F) -> Self {
MapResult { f, inner }
}

Expand Down Expand Up @@ -79,7 +79,7 @@ where

impl<F> MapResultLayer<F> {
/// Creates a new [`MapResultLayer`] layer.
pub fn new(f: F) -> Self {
pub const fn new(f: F) -> Self {
MapResultLayer { f }
}
}
Expand Down
6 changes: 3 additions & 3 deletions tower/src/util/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pin_project! {
}

impl<S: Service<Req>, Req> State<S, Req> {
fn not_ready(svc: S, req: Option<Req>) -> Self {
const fn not_ready(svc: S, req: Option<Req>) -> Self {
Self::NotReady { svc, req }
}

fn called(fut: S::Future) -> Self {
const fn called(fut: S::Future) -> Self {
Self::Called { fut }
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ where
S: Service<Req>,
{
#[allow(missing_docs)]
pub fn new(svc: S, req: Req) -> Self {
pub const fn new(svc: S, req: Req) -> Self {
Oneshot {
state: State::not_ready(svc, Some(req)),
}
Expand Down
Loading

0 comments on commit bd5db7f

Please sign in to comment.