@@ -302,54 +302,41 @@ impl<State> Request<State> {
302302 }
303303
304304 /// Parse the URL query component into a struct, using [serde_qs](https://docs.rs/serde_qs). To
305- /// get the entire query as an unparsed string, use `request.url().query()`
305+ /// get the entire query as an unparsed string, use `request.url().query()`.
306306 ///
307- /// ```rust
308- /// # fn main() -> Result<(), std::io::Error> { async_std::task::block_on(async {
309- /// use tide::prelude::*;
310- /// let mut app = tide::new();
307+ /// # Examples
311308 ///
312- /// #[derive(Deserialize)]
313- /// #[serde(default)]
314- /// struct Page {
315- /// size: u8,
316- /// offset: u8,
317- /// }
318- /// impl Default for Page {
319- /// fn default() -> Self {
320- /// Self {
321- /// size: 25,
322- /// offset: 0,
323- /// }
324- /// }
325- /// }
326- /// app.at("/pages").post(|req: tide::Request<()>| async move {
327- /// let page: Page = req.query()?;
328- /// Ok(format!("page {}, with {} items", page.offset, page.size))
329- /// });
309+ /// ```
310+ /// use std::collections::HashMap;
311+ /// use tide::http::{self, convert::Deserialize};
312+ /// use tide::Request;
330313 ///
331- /// # if false {
332- /// app.listen("localhost:8000").await?;
333- /// # }
314+ /// // An owned structure:
334315 ///
335- /// // $ curl localhost:8000/pages
336- /// // page 0, with 25 items
316+ /// #[derive(Deserialize)]
317+ /// struct Index {
318+ /// page: u32,
319+ /// selections: HashMap<String, String>,
320+ /// }
337321 ///
338- /// // $ curl localhost:8000/pages?offset=1
339- /// // page 1, with 25 items
322+ /// let req: Request<()> = http::Request::get("https://httpbin.org/get?page=2&selections[width]=narrow&selections[height]=tall").into();
323+ /// let Index { page, selections } = req.query().unwrap();
324+ /// assert_eq!(page, 2);
325+ /// assert_eq!(selections["width"], "narrow");
326+ /// assert_eq!(selections["height"], "tall");
340327 ///
341- /// // $ curl localhost:8000/pages?offset=2&size=50
342- /// // page 2, with 50 items
328+ /// // Using borrows:
343329 ///
344- /// // $ curl localhost:8000/pages?size=5000
345- /// // failed with reason: number too large to fit in target type
330+ /// #[derive(Deserialize)]
331+ /// struct Query<'q> {
332+ /// format: &'q str,
333+ /// }
346334 ///
347- /// // $ curl localhost:8000/pages?size=all
348- /// // failed with reason: invalid digit found in string
349- /// # Ok(()) })}
335+ /// let req: Request<()> = http::Request::get("https://httpbin.org/get?format=bananna").into();
336+ /// let Query { format } = req.query().unwrap();
337+ /// assert_eq!(format, "bananna");
350338 /// ```
351-
352- pub fn query < T : serde:: de:: DeserializeOwned > ( & self ) -> crate :: Result < T > {
339+ pub fn query < ' de , T : serde:: de:: Deserialize < ' de > > ( & ' de self ) -> crate :: Result < T > {
353340 self . req . query ( )
354341 }
355342
0 commit comments