Skip to content

Commit

Permalink
Add extensions_ref() and extensions_mut() to req/resp builders (h…
Browse files Browse the repository at this point in the history
…yperium#403)

A similar capability already exists for many of the other `Parts` fields in these builders, and
is useful for the extensions map as well.
  • Loading branch information
acfoltzer committed Mar 25, 2020
1 parent 8b609f9 commit 26681dd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/request.rs
Expand Up @@ -976,6 +976,41 @@ impl Builder {
})
}

/// Get a reference to the extensions for this request builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use http::Request;
/// let req = Request::builder().extension("My Extension").extension(5u32);
/// let extensions = req.extensions_ref().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_ref(&self) -> Option<&Extensions> {
self.inner.as_ref().ok().map(|h| &h.extensions)
}

/// Get a mutable reference to the extensions for this request builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use http::Request;
/// let mut req = Request::builder().extension("My Extension");
/// let mut extensions = req.extensions_mut().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// extensions.insert(5u32);
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
self.inner.as_mut().ok().map(|h| &mut h.extensions)
}

/// "Consumes" this builder, using the provided `body` to return a
/// constructed `Request`.
///
Expand Down
35 changes: 35 additions & 0 deletions src/response.rs
Expand Up @@ -698,6 +698,41 @@ impl Builder {
})
}

/// Get a reference to the extensions for this response builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use http::Response;
/// let req = Response::builder().extension("My Extension").extension(5u32);
/// let extensions = req.extensions_ref().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_ref(&self) -> Option<&Extensions> {
self.inner.as_ref().ok().map(|h| &h.extensions)
}

/// Get a mutable reference to the extensions for this response builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use http::Response;
/// let mut req = Response::builder().extension("My Extension");
/// let mut extensions = req.extensions_mut().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// extensions.insert(5u32);
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
self.inner.as_mut().ok().map(|h| &mut h.extensions)
}

/// "Consumes" this builder, using the provided `body` to return a
/// constructed `Response`.
///
Expand Down

0 comments on commit 26681dd

Please sign in to comment.