Skip to content

Commit 09c0e28

Browse files
committed
Turn crate into no_std using core + alloc
Extensions are also made an optional feature to not require depending on HashMap
1 parent 34a9d6b commit 09c0e28

21 files changed

+338
-242
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ rust-version = "1.49.0"
2727
bytes = "1"
2828
fnv = "1.0.5"
2929
itoa = "1"
30+
ahash = { version = "0.7", default-features = false }
31+
hashbrown = { version = "0.12", optional = true }
32+
33+
[features]
34+
default = ["std", "extensions"]
35+
std = []
36+
hashmap = ["hashbrown"]
37+
extensions = ["hashmap"]
3038

3139
[dev-dependencies]
3240
indexmap = "<=1.8"

src/byte_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use alloc::string::String;
12
use bytes::Bytes;
2-
3-
use std::{ops, str};
3+
use core::{ops, str};
44

55
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
66
pub(crate) struct ByteStr {

src/convert.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
macro_rules! if_downcast_into {
2-
($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => ({
3-
if std::any::TypeId::of::<$in_ty>() == std::any::TypeId::of::<$out_ty>() {
2+
($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => {{
3+
if core::any::TypeId::of::<$in_ty>() == core::any::TypeId::of::<$out_ty>() {
44
// Store the value in an `Option` so we can `take`
55
// it after casting to `&mut dyn Any`.
66
let mut slot = Some($val);
77
// Re-write the `$val` ident with the downcasted value.
8-
let $val = (&mut slot as &mut dyn std::any::Any)
8+
let $val = (&mut slot as &mut dyn core::any::Any)
99
.downcast_mut::<Option<$out_ty>>()
1010
.unwrap()
1111
.take()
1212
.unwrap();
1313
// Run the $body in scope of the replaced val.
1414
$body
1515
}
16-
})
16+
}};
1717
}

src/error.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use core::fmt;
2+
use core::result;
3+
#[cfg(feature = "std")]
14
use std::error;
2-
use std::fmt;
3-
use std::result;
45

56
use crate::header;
67
use crate::method;
@@ -31,19 +32,38 @@ enum ErrorKind {
3132

3233
impl fmt::Debug for Error {
3334
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35+
use ErrorKind::*;
36+
let e: &dyn fmt::Debug = match self.inner {
37+
StatusCode(ref e) => e,
38+
Method(ref e) => e,
39+
Uri(ref e) => e,
40+
UriParts(ref e) => e,
41+
HeaderName(ref e) => e,
42+
HeaderValue(ref e) => e,
43+
};
3444
f.debug_tuple("http::Error")
3545
// Skip the noise of the ErrorKind enum
36-
.field(&self.get_ref())
46+
.field(e)
3747
.finish()
3848
}
3949
}
4050

4151
impl fmt::Display for Error {
4252
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43-
fmt::Display::fmt(self.get_ref(), f)
53+
use ErrorKind::*;
54+
let e: &dyn fmt::Display = match self.inner {
55+
StatusCode(ref e) => e,
56+
Method(ref e) => e,
57+
Uri(ref e) => e,
58+
UriParts(ref e) => e,
59+
HeaderName(ref e) => e,
60+
HeaderValue(ref e) => e,
61+
};
62+
fmt::Display::fmt(e, f)
4463
}
4564
}
4665

66+
#[cfg(feature = "std")]
4767
impl Error {
4868
/// Return true if the underlying error has the same type as T.
4969
pub fn is<T: error::Error + 'static>(&self) -> bool {
@@ -65,6 +85,7 @@ impl Error {
6585
}
6686
}
6787

88+
#[cfg(feature = "std")]
6889
impl error::Error for Error {
6990
// Return any available cause from the inner error. Note the inner error is
7091
// not itself the cause.
@@ -121,13 +142,13 @@ impl From<header::InvalidHeaderValue> for Error {
121142
}
122143
}
123144

124-
impl From<std::convert::Infallible> for Error {
125-
fn from(err: std::convert::Infallible) -> Error {
145+
impl From<core::convert::Infallible> for Error {
146+
fn from(err: core::convert::Infallible) -> Error {
126147
match err {}
127148
}
128149
}
129150

130-
#[cfg(test)]
151+
#[cfg(all(test, feature = "std"))]
131152
mod tests {
132153
use super::*;
133154

src/extensions.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use std::any::{Any, TypeId};
2-
use std::collections::HashMap;
3-
use std::fmt;
4-
use std::hash::{BuildHasherDefault, Hasher};
1+
use core::any::{Any, TypeId};
2+
use core::fmt;
3+
use core::hash::{BuildHasherDefault, Hasher};
4+
use hashbrown::HashMap;
5+
6+
use alloc::boxed::Box;
57

68
type AnyMap = HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>;
79

@@ -166,9 +168,7 @@ impl Extensions {
166168
/// ```
167169
#[inline]
168170
pub fn is_empty(&self) -> bool {
169-
self.map
170-
.as_ref()
171-
.map_or(true, |map| map.is_empty())
171+
self.map.as_ref().map_or(true, |map| map.is_empty())
172172
}
173173

174174
/// Get the numer of extensions available.
@@ -184,28 +184,26 @@ impl Extensions {
184184
/// ```
185185
#[inline]
186186
pub fn len(&self) -> usize {
187-
self.map
188-
.as_ref()
189-
.map_or(0, |map| map.len())
187+
self.map.as_ref().map_or(0, |map| map.len())
190188
}
191189

192190
/// Extends `self` with another `Extensions`.
193191
///
194192
/// If an instance of a specific type exists in both, the one in `self` is overwritten with the
195193
/// one from `other`.
196-
///
194+
///
197195
/// # Example
198-
///
196+
///
199197
/// ```
200198
/// # use http::Extensions;
201199
/// let mut ext_a = Extensions::new();
202200
/// ext_a.insert(8u8);
203201
/// ext_a.insert(16u16);
204-
///
202+
///
205203
/// let mut ext_b = Extensions::new();
206204
/// ext_b.insert(4u8);
207205
/// ext_b.insert("hello");
208-
///
206+
///
209207
/// ext_a.extend(ext_b);
210208
/// assert_eq!(ext_a.len(), 3);
211209
/// assert_eq!(ext_a.get::<u8>(), Some(&4u8));

0 commit comments

Comments
 (0)