Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make new functions const when possible #760

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tower-layer/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Identity {

impl Identity {
/// Create a new [`Identity`] value
pub fn new() -> Identity {
pub const fn new() -> Identity {
Identity { _p: () }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tower-layer/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Stack<Inner, Outer> {

impl<Inner, Outer> Stack<Inner, Outer> {
/// Create a new `Stack`.
pub fn new(inner: Inner, outer: Outer) -> Self {
pub const fn new(inner: Inner, outer: Outer) -> Self {
Stack { inner, outer }
}
}
Expand Down
4 changes: 2 additions & 2 deletions tower-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ use std::task::{Context, Poll};
/// }
///
/// impl<T> Timeout<T> {
/// pub fn new(inner: T, timeout: Duration) -> Timeout<T> {
/// pub const fn new(inner: T, timeout: Duration) -> Timeout<T> {
/// Timeout {
/// inner,
/// timeout
Expand Down Expand Up @@ -204,7 +204,7 @@ use std::task::{Context, Poll};
/// pub struct TimeoutLayer(Duration);
///
/// impl TimeoutLayer {
/// pub fn new(delay: Duration) -> Self {
/// pub const fn new(delay: Duration) -> Self {
/// TimeoutLayer(delay)
/// }
/// }
Expand Down
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
2 changes: 1 addition & 1 deletion tower/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Default for ServiceBuilder<Identity> {

impl ServiceBuilder<Identity> {
/// Create a new [`ServiceBuilder`].
pub fn new() -> Self {
pub const fn new() -> Self {
ServiceBuilder {
layer: Identity::new(),
}
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.as_nanos() > 0);

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

/// Tracks an in-flight request by reference count.
#[derive(Debug)]
pub struct Handle(RefCount);

Check warning on line 48 in tower/src/load/pending_requests.rs

View workflow job for this annotation

GitHub Actions / check-stable

field `0` is never read

Check warning on line 48 in tower/src/load/pending_requests.rs

View workflow job for this annotation

GitHub Actions / test-versions (stable)

field `0` is never read

Check warning on line 48 in tower/src/load/pending_requests.rs

View workflow job for this annotation

GitHub Actions / test-versions (nightly)

field `0` is never read

Check warning on line 48 in tower/src/load/pending_requests.rs

View workflow job for this annotation

GitHub Actions / test-versions (beta)

field `0` is never read

// ===== impl PendingRequests =====

Expand Down Expand Up @@ -100,7 +100,7 @@
#[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
4 changes: 2 additions & 2 deletions 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(mk_service: M, target: Target) -> Self {
pub const fn new(mk_service: M, target: Target) -> Self {
Reconnect {
mk_service,
state: State::Idle,
Expand All @@ -59,7 +59,7 @@ where
}

/// Reconnect to a already connected [`Service`].
pub fn with_connection(init_conn: M::Response, mk_service: M, target: Target) -> Self {
pub const fn with_connection(init_conn: M::Response, mk_service: M, target: Target) -> Self {
Reconnect {
mk_service,
state: State::Connected(init_conn),
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
Loading
Loading