Remove bitflags crate dependency (#2073)

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
This commit is contained in:
Rodrigo Santiago 2023-07-05 16:08:11 -04:00 committed by GitHub
parent 8cb11e7f94
commit b80d126137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 32 deletions

View File

@ -109,7 +109,10 @@ where
T: 'static, T: 'static,
{ {
let path = self.show_update_destroy_path(); let path = self.show_update_destroy_path();
self.route(&path, on(MethodFilter::PUT | MethodFilter::PATCH, handler)) self.route(
&path,
on(MethodFilter::PUT.or(MethodFilter::PATCH), handler),
)
} }
/// Add a handler at `DELETE /{resource_name}/:{resource_name}_id`. /// Add a handler at `DELETE /{resource_name}/:{resource_name}_id`.

View File

@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **fixed:** Don't remove the `Sec-WebSocket-Key` header in `WebSocketUpgrade` ([#1972]) - **fixed:** Don't remove the `Sec-WebSocket-Key` header in `WebSocketUpgrade` ([#1972])
- **added:** Add `axum::extract::Query::try_from_uri` ([#2058]) - **added:** Add `axum::extract::Query::try_from_uri` ([#2058])
- **added:** Implement `IntoResponse` for `Box<str>` and `Box<[u8]>` ([#2035]) - **added:** Implement `IntoResponse` for `Box<str>` and `Box<[u8]>` ([#2035])
- **breaking:** Simplify `MethodFilter`. It no longer uses bitflags ([#2073])
[#1664]: https://github.com/tokio-rs/axum/pull/1664 [#1664]: https://github.com/tokio-rs/axum/pull/1664
[#1751]: https://github.com/tokio-rs/axum/pull/1751 [#1751]: https://github.com/tokio-rs/axum/pull/1751
@ -66,6 +67,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1956]: https://github.com/tokio-rs/axum/pull/1956 [#1956]: https://github.com/tokio-rs/axum/pull/1956
[#1972]: https://github.com/tokio-rs/axum/pull/1972 [#1972]: https://github.com/tokio-rs/axum/pull/1972
[#2058]: https://github.com/tokio-rs/axum/pull/2058 [#2058]: https://github.com/tokio-rs/axum/pull/2058
[#2073]: https://github.com/tokio-rs/axum/pull/2073
# 0.6.17 (25. April, 2023) # 0.6.17 (25. April, 2023)

View File

@ -33,7 +33,6 @@ __private_docs = ["tower/full", "dep:tower-http"]
[dependencies] [dependencies]
async-trait = "0.1.67" async-trait = "0.1.67"
axum-core = { path = "../axum-core", version = "0.3.4" } axum-core = { path = "../axum-core", version = "0.3.4" }
bitflags = "1.0"
bytes = "1.0" bytes = "1.0"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] } futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
http = "0.2.9" http = "0.2.9"

View File

@ -235,7 +235,7 @@ mod tests {
let app = Router::new().route( let app = Router::new().route(
"/", "/",
on( on(
MethodFilter::GET | MethodFilter::POST, MethodFilter::GET.or(MethodFilter::POST),
|_: Form<Payload>| async {}, |_: Form<Payload>| async {},
), ),
); );

View File

@ -372,13 +372,35 @@ impl Event {
} }
} }
bitflags::bitflags! { #[derive(Default, Debug, Copy, Clone, PartialEq)]
#[derive(Default)] struct EventFlags(u8);
struct EventFlags: u8 {
const HAS_DATA = 0b0001; impl EventFlags {
const HAS_EVENT = 0b0010; const HAS_DATA: Self = Self::from_bits(0b0001);
const HAS_RETRY = 0b0100; const HAS_EVENT: Self = Self::from_bits(0b0010);
const HAS_ID = 0b1000; const HAS_RETRY: Self = Self::from_bits(0b0100);
const HAS_ID: Self = Self::from_bits(0b1000);
const fn bits(&self) -> u8 {
let bits = self;
bits.0
}
const fn from_bits(bits: u8) -> Self {
let bits = bits;
Self(bits)
}
const fn contains(&self, other: Self) -> bool {
let same = self;
let other = other;
same.bits() & other.bits() == other.bits()
}
fn insert(&mut self, other: Self) {
let same = self;
let other = other;
*same = Self::from_bits(same.bits() | other.bits());
} }
} }

View File

@ -1,29 +1,50 @@
use bitflags::bitflags;
use http::Method; use http::Method;
use std::{ use std::{
fmt, fmt,
fmt::{Debug, Formatter}, fmt::{Debug, Formatter},
}; };
bitflags! { /// A filter that matches one or more HTTP methods.
/// A filter that matches one or more HTTP methods. #[derive(Debug, Copy, Clone, PartialEq)]
pub struct MethodFilter: u16 { pub struct MethodFilter(u16);
/// Match `DELETE` requests.
const DELETE = 0b000000010; impl MethodFilter {
/// Match `GET` requests. /// Match `DELETE` requests.
const GET = 0b000000100; pub const DELETE: Self = Self::from_bits(0b000000010);
/// Match `HEAD` requests. /// Match `GET` requests.
const HEAD = 0b000001000; pub const GET: Self = Self::from_bits(0b000000100);
/// Match `OPTIONS` requests. /// Match `HEAD` requests.
const OPTIONS = 0b000010000; pub const HEAD: Self = Self::from_bits(0b000001000);
/// Match `PATCH` requests. /// Match `OPTIONS` requests.
const PATCH = 0b000100000; pub const OPTIONS: Self = Self::from_bits(0b000010000);
/// Match `POST` requests. /// Match `PATCH` requests.
const POST = 0b001000000; pub const PATCH: Self = Self::from_bits(0b000100000);
/// Match `PUT` requests. /// Match `POST` requests.
const PUT = 0b010000000; pub const POST: Self = Self::from_bits(0b001000000);
/// Match `TRACE` requests. /// Match `PUT` requests.
const TRACE = 0b100000000; pub const PUT: Self = Self::from_bits(0b010000000);
/// Match `TRACE` requests.
pub const TRACE: Self = Self::from_bits(0b100000000);
const fn bits(&self) -> u16 {
let bits = self;
bits.0
}
const fn from_bits(bits: u16) -> Self {
let bits = bits;
Self(bits)
}
pub(crate) const fn contains(&self, other: Self) -> bool {
let same = self;
let other = other;
same.bits() & other.bits() == other.bits()
}
/// Performs the OR operation between the [`MethodFilter`] in `self` with `other`.
pub const fn or(self, other: Self) -> Self {
Self(self.0 | other.0)
} }
} }

View File

@ -284,7 +284,7 @@ async fn multiple_methods_for_one_handler() {
"Hello, World!" "Hello, World!"
} }
let app = Router::new().route("/", on(MethodFilter::GET | MethodFilter::POST, root)); let app = Router::new().route("/", on(MethodFilter::GET.or(MethodFilter::POST), root));
let client = TestClient::new(app); let client = TestClient::new(app);

View File

@ -28,7 +28,9 @@ skip-tree = [
# until 1.0 is out we're pulling in both 0.14 and 1.0-rc.x # until 1.0 is out we're pulling in both 0.14 and 1.0-rc.x
{ name = "hyper" }, { name = "hyper" },
# pulled in by tracing-subscriber # pulled in by tracing-subscriber
{ name = "regex-syntax" } { name = "regex-syntax" },
# pulled in by tracing-subscriber
{ name = "regex-automata" },
] ]
[sources] [sources]