Skip to content
New issue

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

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

Already on GitHub? # to your account

Remove bitflags crate dependency #2073

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ __private_docs = ["tower/full", "dep:tower-http"]
[dependencies]
async-trait = "0.1.67"
axum-core = { path = "../axum-core", version = "0.3.4" }
bitflags = "1.0"
bytes = "1.0"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
http = "0.2.9"
Expand Down
51 changes: 44 additions & 7 deletions axum/src/response/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,50 @@ impl Event {
}
}

bitflags::bitflags! {
#[derive(Default)]
struct EventFlags: u8 {
const HAS_DATA = 0b0001;
const HAS_EVENT = 0b0010;
const HAS_RETRY = 0b0100;
const HAS_ID = 0b1000;
#[derive(Default, Debug, Copy, Clone, PartialEq)]
struct EventFlags(u8);

impl EventFlags {
const HAS_DATA: Self = Self::from_bits(0b0001);
const HAS_EVENT: Self = Self::from_bits(0b0010);
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());
}
}
}
impl std::ops::BitOr for EventFlags {
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}

Expand Down
69 changes: 49 additions & 20 deletions axum/src/routing/method_filter.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
use bitflags::bitflags;
use http::Method;
use std::{
fmt,
fmt::{Debug, Formatter},
};

bitflags! {
/// A filter that matches one or more HTTP methods.
pub struct MethodFilter: u16 {
/// Match `DELETE` requests.
const DELETE = 0b000000010;
/// Match `GET` requests.
const GET = 0b000000100;
/// Match `HEAD` requests.
const HEAD = 0b000001000;
/// Match `OPTIONS` requests.
const OPTIONS = 0b000010000;
/// Match `PATCH` requests.
const PATCH = 0b000100000;
/// Match `POST` requests.
const POST = 0b001000000;
/// Match `PUT` requests.
const PUT = 0b010000000;
/// Match `TRACE` requests.
const TRACE = 0b100000000;
/// A filter that matches one or more HTTP methods.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct MethodFilter(u16);

impl MethodFilter {
/// Match `DELETE` requests.
pub const DELETE: Self = Self::from_bits(0b000000010);
/// Match `GET` requests.
pub const GET: Self = Self::from_bits(0b000000100);
/// Match `HEAD` requests.
pub const HEAD: Self = Self::from_bits(0b000001000);
/// Match `OPTIONS` requests.
pub const OPTIONS: Self = Self::from_bits(0b000010000);
/// Match `PATCH` requests.
pub const PATCH: Self = Self::from_bits(0b000100000);
/// Match `POST` requests.
pub const POST: Self = Self::from_bits(0b001000000);
/// Match `PUT` requests.
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()
}
}
rsdlt marked this conversation as resolved.
Show resolved Hide resolved
}
impl std::ops::BitOr for MethodFilter {
rsdlt marked this conversation as resolved.
Show resolved Hide resolved
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}

Expand Down