Skip to content

Commit

Permalink
style(lib): add rustfmt::skip attribute to some place and apply rustf…
Browse files Browse the repository at this point in the history
…mt (#645)

* style(lib): add rustfmt::skip attribute to some place and apply rustfmt

* chore(ci): check rustfmt
  • Loading branch information
tottoto authored Nov 15, 2023
1 parent 1c06623 commit 453cd1b
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 148 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ env:
RUST_BACKTRACE: 1

jobs:
style:
name: Check Style
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt

- run: cargo fmt --all --check

test:
name: Test ${{ matrix.rust }}
#needs: [style]
Expand Down
4 changes: 2 additions & 2 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! if_downcast_into {
($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => ({
($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => {{
if std::any::TypeId::of::<$in_ty>() == std::any::TypeId::of::<$out_ty>() {
// Store the value in an `Option` so we can `take`
// it after casting to `&mut dyn Any`.
Expand All @@ -13,5 +13,5 @@ macro_rules! if_downcast_into {
// Run the $body in scope of the replaced val.
$body
}
})
}};
}
32 changes: 8 additions & 24 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ impl Extensions {
self.map
.get_or_insert_with(|| Box::new(HashMap::default()))
.insert(TypeId::of::<T>(), Box::new(val))
.and_then(|boxed| {
boxed.into_any()
.downcast()
.ok()
.map(|boxed| *boxed)
})
.and_then(|boxed| boxed.into_any().downcast().ok().map(|boxed| *boxed))
}

/// Get a reference to a type previously inserted on this `Extensions`.
Expand Down Expand Up @@ -126,12 +121,7 @@ impl Extensions {
self.map
.as_mut()
.and_then(|map| map.remove(&TypeId::of::<T>()))
.and_then(|boxed| {
boxed.into_any()
.downcast()
.ok()
.map(|boxed| *boxed)
})
.and_then(|boxed| boxed.into_any().downcast().ok().map(|boxed| *boxed))
}

/// Clear the `Extensions` of all inserted extensions.
Expand Down Expand Up @@ -166,9 +156,7 @@ impl Extensions {
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.map
.as_ref()
.map_or(true, |map| map.is_empty())
self.map.as_ref().map_or(true, |map| map.is_empty())
}

/// Get the numer of extensions available.
Expand All @@ -184,28 +172,26 @@ impl Extensions {
/// ```
#[inline]
pub fn len(&self) -> usize {
self.map
.as_ref()
.map_or(0, |map| map.len())
self.map.as_ref().map_or(0, |map| map.len())
}

/// Extends `self` with another `Extensions`.
///
/// If an instance of a specific type exists in both, the one in `self` is overwritten with the
/// one from `other`.
///
///
/// # Example
///
///
/// ```
/// # use http::Extensions;
/// let mut ext_a = Extensions::new();
/// ext_a.insert(8u8);
/// ext_a.insert(16u16);
///
///
/// let mut ext_b = Extensions::new();
/// ext_b.insert(4u8);
/// ext_b.insert("hello");
///
///
/// ext_a.extend(ext_b);
/// assert_eq!(ext_a.len(), 3);
/// assert_eq!(ext_a.get::<u8>(), Some(&4u8));
Expand Down Expand Up @@ -260,8 +246,6 @@ impl Clone for Box<dyn AnyClone + Send + Sync> {
}
}



#[test]
fn test_extensions() {
#[derive(Clone, Debug, PartialEq)]
Expand Down
67 changes: 28 additions & 39 deletions src/header/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::{FromIterator, FusedIterator};
Expand All @@ -8,8 +8,8 @@ use std::{fmt, mem, ops, ptr, vec};

use crate::Error;

use super::HeaderValue;
use super::name::{HdrName, HeaderName, InvalidHeaderName};
use super::HeaderValue;

pub use self::as_header_name::AsHeaderName;
pub use self::into_header_name::IntoHeaderName;
Expand Down Expand Up @@ -454,9 +454,9 @@ impl<T> HeaderMap<T> {
/// allocations before `capacity` headers are stored in the map.
///
/// More capacity than requested may be allocated.
///
///
/// # Panics
///
///
/// Requested capacity too large: would overflow `usize`.
///
/// # Examples
Expand Down Expand Up @@ -970,7 +970,9 @@ impl<T> HeaderMap<T> {
let entries = &mut self.entries[..] as *mut _;
let extra_values = &mut self.extra_values as *mut _;
let len = self.entries.len();
unsafe { self.entries.set_len(0); }
unsafe {
self.entries.set_len(0);
}

Drain {
idx: 0,
Expand Down Expand Up @@ -1202,10 +1204,8 @@ impl<T> HeaderMap<T> {
let raw_links = self.raw_links();
let extra_values = &mut self.extra_values;

let next = links.map(|l| {
drain_all_extra_values(raw_links, extra_values, l.next)
.into_iter()
});
let next =
links.map(|l| drain_all_extra_values(raw_links, extra_values, l.next).into_iter());

ValueDrain {
first: Some(old),
Expand Down Expand Up @@ -1604,9 +1604,8 @@ impl<T> HeaderMap<T> {
fn remove_extra_value<T>(
mut raw_links: RawLinks<T>,
extra_values: &mut Vec<ExtraValue<T>>,
idx: usize)
-> ExtraValue<T>
{
idx: usize,
) -> ExtraValue<T> {
let prev;
let next;

Expand All @@ -1627,17 +1626,15 @@ fn remove_extra_value<T>(
(Link::Entry(prev), Link::Extra(next)) => {
debug_assert!(raw_links[prev].is_some());

raw_links[prev].as_mut().unwrap()
.next = next;
raw_links[prev].as_mut().unwrap().next = next;

debug_assert!(extra_values.len() > next);
extra_values[next].prev = Link::Entry(prev);
}
(Link::Extra(prev), Link::Entry(next)) => {
debug_assert!(raw_links[next].is_some());

raw_links[next].as_mut().unwrap()
.tail = prev;
raw_links[next].as_mut().unwrap().tail = prev;

debug_assert!(extra_values.len() > prev);
extra_values[prev].next = Link::Entry(next);
Expand Down Expand Up @@ -1725,9 +1722,8 @@ fn remove_extra_value<T>(
fn drain_all_extra_values<T>(
raw_links: RawLinks<T>,
extra_values: &mut Vec<ExtraValue<T>>,
mut head: usize)
-> Vec<T>
{
mut head: usize,
) -> Vec<T> {
let mut vec = Vec::new();
loop {
let extra = remove_extra_value(raw_links, extra_values, head);
Expand Down Expand Up @@ -1849,12 +1845,12 @@ impl<T> FromIterator<(HeaderName, T)> for HeaderMap<T> {
/// assert_eq!(headers["X-Custom-Header"], "my value");
/// ```
impl<'a, K, V, T> TryFrom<&'a HashMap<K, V>> for HeaderMap<T>
where
K: Eq + Hash,
HeaderName: TryFrom<&'a K>,
<HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
T: TryFrom<&'a V>,
T::Error: Into<crate::Error>,
where
K: Eq + Hash,
HeaderName: TryFrom<&'a K>,
<HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
T: TryFrom<&'a V>,
T::Error: Into<crate::Error>,
{
type Error = Error;

Expand Down Expand Up @@ -2247,9 +2243,7 @@ impl<'a, T> Iterator for Drain<'a, T> {
// Remove the extra value

let raw_links = RawLinks(self.entries);
let extra = unsafe {
remove_extra_value(raw_links, &mut *self.extra_values, next)
};
let extra = unsafe { remove_extra_value(raw_links, &mut *self.extra_values, next) };

match extra.next {
Link::Extra(idx) => self.next = Some(idx),
Expand Down Expand Up @@ -3012,10 +3006,9 @@ impl<'a, T> OccupiedEntry<'a, T> {
let raw_links = self.map.raw_links();
let extra_values = &mut self.map.extra_values;

let next = self.map.entries[self.index].links.map(|l| {
drain_all_extra_values(raw_links, extra_values, l.next)
.into_iter()
});
let next = self.map.entries[self.index]
.links
.map(|l| drain_all_extra_values(raw_links, extra_values, l.next).into_iter());

let entry = self.map.remove_found(self.probe, self.index);

Expand Down Expand Up @@ -3129,7 +3122,7 @@ impl<'a, T> Iterator for ValueDrain<'a, T> {
(&Some(_), &Some(ref extras)) => {
let (l, u) = extras.size_hint();
(l + 1, u.map(|u| u + 1))
},
}
// Extras only
(&None, &Some(ref extras)) => extras.size_hint(),
// No more
Expand Down Expand Up @@ -3163,17 +3156,13 @@ impl<T> ops::Index<usize> for RawLinks<T> {
type Output = Option<Links>;

fn index(&self, idx: usize) -> &Self::Output {
unsafe {
&(*self.0)[idx].links
}
unsafe { &(*self.0)[idx].links }
}
}

impl<T> ops::IndexMut<usize> for RawLinks<T> {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
unsafe {
&mut (*self.0)[idx].links
}
unsafe { &mut (*self.0)[idx].links }
}
}

Expand Down
1 change: 1 addition & 0 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub use self::name::{HeaderName, InvalidHeaderName};
pub use self::value::{HeaderValue, InvalidHeaderValue, ToStrError};

// Use header name constants
#[rustfmt::skip]
pub use self::name::{
ACCEPT,
ACCEPT_CHARSET,
Expand Down
Loading

0 comments on commit 453cd1b

Please sign in to comment.