Skip to content

Commit

Permalink
Chore: A few clippy inspired changes
Browse files Browse the repository at this point in the history
* Combine identical `match` branches into one statement with a `|` symbol
* Use `_` in a 9-bit binary values (I didn't even realize they were 9-bit instead of the common 8-bit until I did this)
* Replaced a few `.map().unwrap_or_else()` with `.map_or_else()`
* Use specific type with `default()` instead of `Default::default()` to make it easier to read/understand
  • Loading branch information
nyurik committed Sep 19, 2023
1 parent a9822ec commit 1fc9924
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion axum-core/src/ext_traits/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl RequestExt for Request {
*req.uri_mut() = self.uri().clone();
*req.headers_mut() = std::mem::take(self.headers_mut());
*req.extensions_mut() = std::mem::take(self.extensions_mut());
let (mut parts, _) = req.into_parts();
let (mut parts, ()) = req.into_parts();

Box::pin(async move {
let result = E::from_request_parts(&mut parts, state).await;
Expand Down
2 changes: 1 addition & 1 deletion axum-extra/src/extract/with_rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<E, R> Copy for WithRejection<E, R> where E: Copy {}

impl<E: Default, R> Default for WithRejection<E, R> {
fn default() -> Self {
Self(Default::default(), Default::default())
Self(E::default(), PhantomData)
}
}

Expand Down
4 changes: 1 addition & 3 deletions axum-extra/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ use tower_layer::Identity;
/// [`HandleErrorLayer`]: axum::error_handling::HandleErrorLayer
/// [`Infallible`]: std::convert::Infallible
pub fn option_layer<L>(layer: Option<L>) -> Either<L, Identity> {
layer
.map(Either::E1)
.unwrap_or_else(|| Either::E2(Identity::new()))
layer.map_or_else(|| Either::E2(Identity::new()), Either::E1)
}
3 changes: 1 addition & 2 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ where
async fn redirect_handler(uri: Uri) -> Response {
let new_uri = map_path(uri, |path| {
path.strip_suffix('/')
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(format!("{path}/")))
.map_or_else(|| Cow::Owned(format!("{path}/")), Cow::Borrowed)
});

if let Some(new_uri) = new_uri {
Expand Down
6 changes: 2 additions & 4 deletions axum-macros/src/from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl State {
/// ```
fn trait_generics(&self) -> impl Iterator<Item = Type> {
match self {
State::Default(inner) => iter::once(inner.clone()),
State::Custom(inner) => iter::once(inner.clone()),
State::Default(inner) | State::Custom(inner) => iter::once(inner.clone()),
State::CannotInfer => iter::once(parse_quote!(S)),
}
}
Expand All @@ -85,8 +84,7 @@ impl State {
impl ToTokens for State {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
State::Custom(inner) => inner.to_tokens(tokens),
State::Default(inner) => inner.to_tokens(tokens),
State::Custom(inner) | State::Default(inner) => inner.to_tokens(tokens),
State::CannotInfer => quote! { S }.to_tokens(tokens),
}
}
Expand Down
16 changes: 8 additions & 8 deletions axum/src/routing/method_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ pub struct MethodFilter(u16);

impl MethodFilter {
/// Match `DELETE` requests.
pub const DELETE: Self = Self::from_bits(0b000000010);
pub const DELETE: Self = Self::from_bits(0b0_0000_0010);
/// Match `GET` requests.
pub const GET: Self = Self::from_bits(0b000000100);
pub const GET: Self = Self::from_bits(0b0_0000_0100);
/// Match `HEAD` requests.
pub const HEAD: Self = Self::from_bits(0b000001000);
pub const HEAD: Self = Self::from_bits(0b0_0000_1000);
/// Match `OPTIONS` requests.
pub const OPTIONS: Self = Self::from_bits(0b000010000);
pub const OPTIONS: Self = Self::from_bits(0b0_0001_0000);
/// Match `PATCH` requests.
pub const PATCH: Self = Self::from_bits(0b000100000);
pub const PATCH: Self = Self::from_bits(0b0_0010_0000);
/// Match `POST` requests.
pub const POST: Self = Self::from_bits(0b001000000);
pub const POST: Self = Self::from_bits(0b0_0100_0000);
/// Match `PUT` requests.
pub const PUT: Self = Self::from_bits(0b010000000);
pub const PUT: Self = Self::from_bits(0b0_1000_0000);
/// Match `TRACE` requests.
pub const TRACE: Self = Self::from_bits(0b100000000);
pub const TRACE: Self = Self::from_bits(0b1_0000_0000);

const fn bits(&self) -> u16 {
let bits = self;
Expand Down
2 changes: 1 addition & 1 deletion axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
/// all requests.
pub fn new() -> Self {
Self {
path_router: Default::default(),
path_router: PathRouter::default(),
fallback_router: PathRouter::new_fallback(),
default_fallback: true,
catch_all_fallback: Fallback::Default(Route::new(NotFound)),
Expand Down
4 changes: 2 additions & 2 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ where
impl<S, const IS_FALLBACK: bool> Default for PathRouter<S, IS_FALLBACK> {
fn default() -> Self {
Self {
routes: Default::default(),
node: Default::default(),
routes: HashMap::default(),
node: Arc::default(),
prev_route_id: RouteId(0),
}
}
Expand Down

0 comments on commit 1fc9924

Please sign in to comment.