Skip to content

Commit 70bd495

Browse files
authored
Merge pull request #118 from http-rs/extensions
Rename TypeMap to Extensions
2 parents 0a33fc7 + 0c0bdae commit 70bd495

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

src/type_map.rs renamed to src/extensions.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ use std::collections::HashMap;
77
use std::fmt;
88
use std::hash::{BuildHasherDefault, Hasher};
99

10-
/// Store and retrieve values by `TypeId`.
10+
/// A type to store extra data inside `Request` and `Response`.
1111
///
12-
/// Map type that allows storing any `Sync + Send + 'static` type. Instances can
13-
/// be retrieved from [`Request::local`](struct.Request.html#method.local) +
14-
/// [`Response::local`](struct.Response.html#method.local) and
15-
/// [`Request::local_mut`](struct.Request.html#method.local_mut) +
16-
/// [`Response::local_mut`](struct.Response.html#method.local_mut).
12+
/// Store and retrieve values by
13+
/// [`TypeId`](https://doc.rust-lang.org/std/any/struct.TypeId.html). This allows
14+
/// storing arbitrary data that implements `Sync + Send + 'static`. This is
15+
/// useful when for example implementing middleware that needs to send values.
1716
#[derive(Default)]
18-
pub struct TypeMap {
17+
pub struct Extensions {
1918
map: Option<HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>>,
2019
}
2120

22-
impl TypeMap {
23-
/// Create an empty `TypeMap`.
21+
impl Extensions {
22+
/// Create an empty `Extensions`.
2423
#[inline]
2524
pub(crate) fn new() -> Self {
2625
Self { map: None }
2726
}
2827

29-
/// Insert a value into this `TypeMap`.
28+
/// Insert a value into this `Extensions`.
3029
///
3130
/// If a value of this type already exists, it will be returned.
3231
pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T> {
@@ -44,23 +43,23 @@ impl TypeMap {
4443
.is_some()
4544
}
4645

47-
/// Get a reference to a value previously inserted on this `TypeMap`.
46+
/// Get a reference to a value previously inserted on this `Extensions`.
4847
pub fn get<T: 'static>(&self) -> Option<&T> {
4948
self.map
5049
.as_ref()
5150
.and_then(|m| m.get(&TypeId::of::<T>()))
5251
.and_then(|boxed| (&**boxed as &(dyn Any)).downcast_ref())
5352
}
5453

55-
/// Get a mutable reference to a value previously inserted on this `TypeMap`.
54+
/// Get a mutable reference to a value previously inserted on this `Extensions`.
5655
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
5756
self.map
5857
.as_mut()
5958
.and_then(|m| m.get_mut(&TypeId::of::<T>()))
6059
.and_then(|boxed| (&mut **boxed as &mut (dyn Any)).downcast_mut())
6160
}
6261

63-
/// Remove a value from this `TypeMap`.
62+
/// Remove a value from this `Extensions`.
6463
///
6564
/// If a value of this type exists, it will be returned.
6665
pub fn remove<T: 'static>(&mut self) -> Option<T> {
@@ -70,16 +69,16 @@ impl TypeMap {
7069
.and_then(|boxed| (boxed as Box<dyn Any>).downcast().ok().map(|boxed| *boxed))
7170
}
7271

73-
/// Clear the `TypeMap` of all inserted values.
72+
/// Clear the `Extensions` of all inserted values.
7473
#[inline]
7574
pub fn clear(&mut self) {
7675
self.map = None;
7776
}
7877
}
7978

80-
impl fmt::Debug for TypeMap {
79+
impl fmt::Debug for Extensions {
8180
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82-
f.debug_struct("TypeMap").finish()
81+
f.debug_struct("Extensions").finish()
8382
}
8483
}
8584

@@ -107,11 +106,11 @@ impl Hasher for IdHasher {
107106
mod tests {
108107
use super::*;
109108
#[test]
110-
fn test_type_map() {
109+
fn test_extensions() {
111110
#[derive(Debug, PartialEq)]
112111
struct MyType(i32);
113112

114-
let mut map = TypeMap::new();
113+
let mut map = Extensions::new();
115114

116115
map.insert(5i32);
117116
map.insert(MyType(10));

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ pub mod mime;
121121

122122
mod body;
123123
mod error;
124+
mod extensions;
124125
mod macros;
125126
mod method;
126127
mod request;
127128
mod response;
128129
mod status;
129130
mod status_code;
130-
mod type_map;
131131
mod version;
132132

133133
cfg_unstable! {
@@ -171,7 +171,7 @@ pub mod security;
171171
mod hyperium_http;
172172

173173
#[doc(inline)]
174-
pub use crate::type_map::TypeMap;
174+
pub use crate::extensions::Extensions;
175175

176176
/// Traits for conversions between types.
177177
pub mod convert {

src/request.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::headers::{
1414
};
1515
use crate::mime::Mime;
1616
use crate::trailers::{self, Trailers};
17-
use crate::{Body, Method, TypeMap, Url, Version};
17+
use crate::{Body, Extensions, Method, Url, Version};
1818

1919
pin_project_lite::pin_project! {
2020
/// An HTTP request.
@@ -37,9 +37,9 @@ pin_project_lite::pin_project! {
3737
receiver: Option<sync::Receiver<Trailers>>,
3838
#[pin]
3939
body: Body,
40-
local: TypeMap,
4140
local_addr: Option<String>,
4241
peer_addr: Option<String>,
42+
ext: Extensions,
4343
}
4444
}
4545

@@ -55,7 +55,7 @@ impl Request {
5555
body: Body::empty(),
5656
sender: Some(sender),
5757
receiver: Some(receiver),
58-
local: TypeMap::new(),
58+
ext: Extensions::new(),
5959
peer_addr: None,
6060
local_addr: None,
6161
}
@@ -539,13 +539,12 @@ impl Request {
539539
}
540540

541541
/// Returns a reference to the existing local state.
542-
pub fn local(&self) -> &TypeMap {
543-
&self.local
542+
pub fn ext(&self) -> &Extensions {
543+
&self.ext
544544
}
545545

546546
/// Returns a mutuable reference to the existing local state.
547547
///
548-
///
549548
/// # Examples
550549
///
551550
/// ```
@@ -554,13 +553,13 @@ impl Request {
554553
/// use http_types::{Url, Method, Request, Version};
555554
///
556555
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
557-
/// req.local_mut().insert("hello from the extension");
558-
/// assert_eq!(req.local().get(), Some(&"hello from the extension"));
556+
/// req.ext_mut().insert("hello from the extension");
557+
/// assert_eq!(req.ext().get(), Some(&"hello from the extension"));
559558
/// #
560559
/// # Ok(()) }
561560
/// ```
562-
pub fn local_mut(&mut self) -> &mut TypeMap {
563-
&mut self.local
561+
pub fn ext_mut(&mut self) -> &mut Extensions {
562+
&mut self.ext
564563
}
565564
}
566565

@@ -575,7 +574,7 @@ impl Clone for Request {
575574
sender: self.sender.clone(),
576575
receiver: self.receiver.clone(),
577576
body: Body::empty(),
578-
local: TypeMap::new(),
577+
ext: Extensions::new(),
579578
peer_addr: self.peer_addr.clone(),
580579
local_addr: self.local_addr.clone(),
581580
}

src/response.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::headers::{
1414
};
1515
use crate::mime::Mime;
1616
use crate::trailers::{self, Trailers};
17-
use crate::{Body, StatusCode, TypeMap, Version};
17+
use crate::{Body, Extensions, StatusCode, Version};
1818

1919
pin_project_lite::pin_project! {
2020
/// An HTTP response.
@@ -40,7 +40,7 @@ pin_project_lite::pin_project! {
4040
receiver: Option<sync::Receiver<Trailers>>,
4141
#[pin]
4242
body: Body,
43-
local: TypeMap,
43+
ext: Extensions,
4444
local_addr: Option<String>,
4545
peer_addr: Option<String>,
4646
}
@@ -57,7 +57,7 @@ impl Response {
5757
body: Body::empty(),
5858
sender: Some(sender),
5959
receiver: Some(receiver),
60-
local: TypeMap::new(),
60+
ext: Extensions::new(),
6161
peer_addr: None,
6262
local_addr: None,
6363
}
@@ -481,8 +481,8 @@ impl Response {
481481
}
482482

483483
/// Returns a reference to the existing local.
484-
pub fn local(&self) -> &TypeMap {
485-
&self.local
484+
pub fn ext(&self) -> &Extensions {
485+
&self.ext
486486
}
487487

488488
/// Returns a mutuable reference to the existing local state.
@@ -496,13 +496,13 @@ impl Response {
496496
/// use http_types::{StatusCode, Response, Version};
497497
///
498498
/// let mut res = Response::new(StatusCode::Ok);
499-
/// res.local_mut().insert("hello from the extension");
500-
/// assert_eq!(res.local().get(), Some(&"hello from the extension"));
499+
/// res.ext_mut().insert("hello from the extension");
500+
/// assert_eq!(res.ext().get(), Some(&"hello from the extension"));
501501
/// #
502502
/// # Ok(()) }
503503
/// ```
504-
pub fn local_mut(&mut self) -> &mut TypeMap {
505-
&mut self.local
504+
pub fn ext_mut(&mut self) -> &mut Extensions {
505+
&mut self.ext
506506
}
507507
}
508508

@@ -516,7 +516,7 @@ impl Clone for Response {
516516
sender: self.sender.clone(),
517517
receiver: self.receiver.clone(),
518518
body: Body::empty(),
519-
local: TypeMap::new(),
519+
ext: Extensions::new(),
520520
peer_addr: self.peer_addr.clone(),
521521
local_addr: self.local_addr.clone(),
522522
}

0 commit comments

Comments
 (0)