Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbr/b0fd2964eb57a1d8d512e4d167ad6554 to your computer and use it in GitHub Desktop.
Save jbr/b0fd2964eb57a1d8d512e4d167ad6554 to your computer and use it in GitHub Desktop.
patch for Fishrock123/tide/server-state-clone
Subject: [PATCH] use pin_project_lite to implement Read for Request
---
Cargo.toml | 1 +
src/request.rs | 39 +++++++++++++++++++--------------------
2 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
index 7efecd2..838c461 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -42,6 +42,7 @@ serde = "1.0.102"
serde_json = "1.0.41"
route-recognizer = "0.2.0"
logtest = "2.0.0"
+pin-project-lite = "0.1.7"
[dev-dependencies]
async-std = { version = "1.6.0", features = ["unstable", "attributes"] }
diff --git a/src/request.rs b/src/request.rs
index 41ec23d..d9ccf3c 100644
--- a/src/request.rs
+++ b/src/request.rs
@@ -4,7 +4,7 @@ use route_recognizer::Params;
use std::ops::Index;
use std::pin::Pin;
-use std::{fmt, str::FromStr, sync::Arc};
+use std::{fmt, str::FromStr};
use crate::cookies::CookieData;
use crate::http::cookies::Cookie;
@@ -12,18 +12,21 @@ use crate::http::headers::{self, HeaderName, HeaderValues, ToHeaderValues};
use crate::http::{self, Body, Method, Mime, StatusCode, Url, Version};
use crate::Response;
-/// An HTTP request.
-///
-/// The `Request` gives endpoints access to basic information about the incoming
-/// request, route parameters, and various ways of accessing the request's body.
-///
-/// Requests also provide *extensions*, a type map primarily used for low-level
-/// communication between middleware and endpoints.
-#[derive(Debug)]
-pub struct Request<State> {
- pub(crate) state: State,
- pub(crate) req: http::Request,
- pub(crate) route_params: Vec<Params>,
+pin_project_lite::pin_project! {
+ /// An HTTP request.
+ ///
+ /// The `Request` gives endpoints access to basic information about the incoming
+ /// request, route parameters, and various ways of accessing the request's body.
+ ///
+ /// Requests also provide *extensions*, a type map primarily used for low-level
+ /// communication between middleware and endpoints.
+ #[derive(Debug)]
+ pub struct Request<State> {
+ pub(crate) state: State,
+ #[pin]
+ pub(crate) req: http::Request,
+ pub(crate) route_params: Vec<Params>,
+ }
}
#[derive(Debug)]
@@ -45,11 +48,7 @@ impl<T: fmt::Debug + fmt::Display> std::error::Error for ParamError<T> {}
impl<State> Request<State> {
/// Create a new `Request`.
- pub(crate) fn new(
- state: State,
- req: http_types::Request,
- route_params: Vec<Params>,
- ) -> Self {
+ pub(crate) fn new(state: State, req: http_types::Request, route_params: Vec<Params>) -> Self {
Self {
state,
req,
@@ -550,11 +549,11 @@ impl<State> AsMut<http::Headers> for Request<State> {
impl<State> Read for Request<State> {
fn poll_read(
- mut self: Pin<&mut Self>,
+ self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
- Pin::new(&mut self.req).poll_read(cx, buf)
+ self.project().req.poll_read(cx, buf)
}
}
--
2.27.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment