Skip to content

Commit 6f3706c

Browse files
authored
Merge pull request #72 from yoshuawuyts/docs-polish
TypeMap polish
2 parents 24cf60a + 00a7299 commit 6f3706c

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ default = []
1616
hyperium_http = ["http"]
1717

1818
[dependencies]
19-
async-std = { version = "1.4.0", features = ["unstable"] }
19+
anyhow = "1.0.26"
2020
cookie = "0.12.0"
2121
infer = "0.1.2"
22+
omnom = "2.1.1"
2223
pin-project-lite = "0.1.0"
2324
url = "2.1.0"
24-
omnom = "2.1.1"
25+
26+
# Note(yoshuawuyts): used for async_std's `channel` only; use "core" once possible.
27+
async-std = { version = "1.4.0", features = ["unstable"] }
2528

2629
# features: hyperium/http
2730
http = { version = "0.2.0", optional = true }
28-
anyhow = "1.0.26"
2931

3032
[dev-dependencies]
3133
http = "0.2.0"

src/request.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pin_project_lite::pin_project! {
3535
receiver: sync::Receiver<crate::Result<Trailers>>,
3636
#[pin]
3737
body: Body,
38-
local_state: TypeMap,
38+
local: TypeMap,
3939
}
4040
}
4141

@@ -51,7 +51,7 @@ impl Request {
5151
body: Body::empty(),
5252
sender: Some(sender),
5353
receiver,
54-
local_state: TypeMap::new(),
54+
local: TypeMap::new(),
5555
}
5656
}
5757

@@ -427,8 +427,8 @@ impl Request {
427427
}
428428

429429
/// Returns a reference to the existing local state.
430-
pub fn local_state(&self) -> &TypeMap {
431-
&self.local_state
430+
pub fn local(&self) -> &TypeMap {
431+
&self.local
432432
}
433433

434434
/// Returns a mutuable reference to the existing local state.
@@ -442,13 +442,13 @@ impl Request {
442442
/// use http_types::{Url, Method, Request, Version};
443443
///
444444
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
445-
/// req.local_state_mut().insert("hello from the extension");
446-
/// assert_eq!(req.local_state().get(), Some(&"hello from the extension"));
445+
/// req.local_mut().insert("hello from the extension");
446+
/// assert_eq!(req.local().get(), Some(&"hello from the extension"));
447447
/// #
448448
/// # Ok(()) }
449449
/// ```
450-
pub fn local_state_mut(&mut self) -> &mut TypeMap {
451-
&mut self.local_state
450+
pub fn local_mut(&mut self) -> &mut TypeMap {
451+
&mut self.local
452452
}
453453
}
454454

src/response.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pin_project_lite::pin_project! {
3737
receiver: sync::Receiver<crate::Result<Trailers>>,
3838
#[pin]
3939
body: Body,
40-
local_state: TypeMap,
40+
local: TypeMap,
4141
}
4242
}
4343

@@ -52,7 +52,7 @@ impl Response {
5252
body: Body::empty(),
5353
sender: Some(sender),
5454
receiver,
55-
local_state: TypeMap::new(),
55+
local: TypeMap::new(),
5656
}
5757
}
5858

@@ -392,9 +392,9 @@ impl Response {
392392
self.headers.values()
393393
}
394394

395-
/// Returns a reference to the existing local_state.
396-
pub fn local_state(&self) -> &TypeMap {
397-
&self.local_state
395+
/// Returns a reference to the existing local.
396+
pub fn local(&self) -> &TypeMap {
397+
&self.local
398398
}
399399

400400
/// Returns a mutuable reference to the existing local state.
@@ -408,13 +408,13 @@ impl Response {
408408
/// use http_types::{StatusCode, Response, Version};
409409
///
410410
/// let mut res = Response::new(StatusCode::Ok);
411-
/// res.local_state_mut().insert("hello from the extension");
412-
/// assert_eq!(res.local_state().get(), Some(&"hello from the extension"));
411+
/// res.local_mut().insert("hello from the extension");
412+
/// assert_eq!(res.local().get(), Some(&"hello from the extension"));
413413
/// #
414414
/// # Ok(()) }
415415
/// ```
416-
pub fn local_state_mut(&mut self) -> &mut TypeMap {
417-
&mut self.local_state
416+
pub fn local_mut(&mut self) -> &mut TypeMap {
417+
&mut self.local
418418
}
419419
}
420420

src/type_map.rs

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

10-
/// Map type that allows storing any `Sync + Send + 'static` type as local state
11-
/// on a `Request` or `Response`.
10+
/// Store and retrieve values by `TypeId`.
11+
///
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).
1217
#[derive(Default)]
1318
pub struct TypeMap {
1419
map: Option<HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>>,
@@ -17,7 +22,7 @@ pub struct TypeMap {
1722
impl TypeMap {
1823
/// Create an empty `TypeMap`.
1924
#[inline]
20-
pub fn new() -> Self {
25+
pub(crate) fn new() -> Self {
2126
Self { map: None }
2227
}
2328

0 commit comments

Comments
 (0)