optimize ServiceRequest methods

This commit is contained in:
fakeshadow 2021-01-03 19:41:27 +08:00
parent ad608aa64e
commit f81506cbc8
2 changed files with 12 additions and 6 deletions

View File

@ -17,6 +17,9 @@ use crate::rmap::ResourceMap;
#[derive(Clone)]
/// An HTTP Request
pub struct HttpRequest(pub(crate) Rc<HttpRequestInner>);
// *. Rc<HttpRequestInner> is used exclusively and NO Weak<HttpRequestInner>
// is allowed anywhere in the code. Weak pointer is purposely ignored when
// doing Rc's ref counter check.
pub(crate) struct HttpRequestInner {
pub(crate) head: Message<RequestHead>,

View File

@ -73,11 +73,12 @@ impl ServiceRequest {
mut req: HttpRequest,
pl: Payload,
) -> Result<Self, (HttpRequest, Payload)> {
if Rc::strong_count(&req.0) == 1 && Rc::weak_count(&req.0) == 0 {
Rc::get_mut(&mut req.0).unwrap().payload = pl;
match Rc::get_mut(&mut req.0) {
Some(p) => {
p.payload = pl;
Ok(ServiceRequest(req))
} else {
Err((req, pl))
}
None => Err((req, pl)),
}
}
@ -87,7 +88,9 @@ impl ServiceRequest {
/// can be re-constructed only if rc's strong pointers count eq 1 and
/// weak pointers count is 0.
pub fn from_request(req: HttpRequest) -> Result<Self, HttpRequest> {
if Rc::strong_count(&req.0) == 1 && Rc::weak_count(&req.0) == 0 {
// There is no weak pointer used on HttpRequest so intentionally
// ignore the check.
if Rc::strong_count(&req.0) == 1 {
Ok(ServiceRequest(req))
} else {
Err(req)