Skip to content
Merged

Lints #704

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --benches --features __internal__bench
args: --benches

- name: tests
uses: actions-rs/cargo@v1
Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ logger = ["femme"]
docs = ["unstable"]
sessions = ["async-session", "cookies"]
unstable = []
# DO NOT USE. Only exists to expose internals so they can be benchmarked.
__internal__bench = []

[dependencies]
async-h1 = { version = "2.1.2", optional = true }
Expand Down
19 changes: 9 additions & 10 deletions benches/router.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use http_types::Method;
use tide::router::Router;
use http_types::{Method, Request, Response, Url};

fn criterion_benchmark(c: &mut Criterion) {
let mut router = Router::<()>::new();
router.add(
"hello",
Method::Get,
Box::new(|_| async { Ok("hello world") }),
);
let mut app = tide::new();
app.at("/hello").get(|_| async { Ok("hello world") });

let route = Url::parse("https://example.com/hello").unwrap();
let req = Request::new(Method::Get, route);
c.bench_function("route-match", |b| {
b.iter(|| black_box(router.route("/hello", Method::Get)))
b.iter(|| black_box(app.respond::<_, Response>(req.clone())));
});

let route = Url::parse("https://example.com").unwrap();
let req = Request::new(Method::Get, route);
c.bench_function("route-root", |b| {
b.iter(|| black_box(router.route("", Method::Get)))
b.iter(|| black_box(app.respond::<_, Response>(req.clone())));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/cookies/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) struct CookiesMiddleware;

impl CookiesMiddleware {
/// Creates a new `CookiesMiddleware`.
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self::default()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
}
}

pub struct MiddlewareEndpoint<E, State> {
pub(crate) struct MiddlewareEndpoint<E, State> {
endpoint: E,
middleware: Vec<Arc<dyn Middleware<State>>>,
}
Expand Down Expand Up @@ -96,7 +96,7 @@ where
State: Clone + Send + Sync + 'static,
E: Endpoint<State>,
{
pub fn wrap_with_middleware(ep: E, middleware: &[Arc<dyn Middleware<State>>]) -> Self {
pub(crate) fn wrap_with_middleware(ep: E, middleware: &[Arc<dyn Middleware<State>>]) -> Self {
Self {
endpoint: ep,
middleware: middleware.to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion src/fs/serve_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_std::path::PathBuf as AsyncPathBuf;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

pub struct ServeDir {
pub(crate) struct ServeDir {
prefix: String,
dir: PathBuf,
}
Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
//! See more examples in the [examples](https://github.com/http-rs/tide/tree/main/examples) directory.

#![cfg_attr(feature = "docs", feature(doc_cfg))]
// #![warn(missing_docs)]
#![warn(missing_debug_implementations, rust_2018_idioms)]
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, unreachable_pub, future_incompatible, rust_2018_idioms)]
#![doc(test(attr(deny(warnings))))]
#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
#![doc(html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico")]
#![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")]
Expand All @@ -70,11 +71,7 @@ mod request;
mod response;
mod response_builder;
mod route;

#[cfg(not(feature = "__internal__bench"))]
mod router;
#[cfg(feature = "__internal__bench")]
pub mod router;
mod server;

pub mod convert;
Expand Down
3 changes: 2 additions & 1 deletion src/listener/to_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ use async_std::io;
/// ```
/// # Other implementations
/// See below for additional provided implementations of ToListener.

pub trait ToListener<State: Clone + Send + Sync + 'static> {
/// What listener are we converting into?
type Listener: Listener<State>;

/// Transform self into a
/// [`Listener`](crate::listener::Listener). Unless self is
/// already bound/connected to the underlying io, converting to a
Expand Down
17 changes: 11 additions & 6 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,43 @@ use crate::{Request, Response, StatusCode};
/// Internally, we have a separate state machine per http method; indexing
/// by the method first allows the table itself to be more efficient.
#[allow(missing_debug_implementations)]
pub struct Router<State> {
pub(crate) struct Router<State> {
method_map: HashMap<http_types::Method, MethodRouter<Box<DynEndpoint<State>>>>,
all_method_router: MethodRouter<Box<DynEndpoint<State>>>,
}

/// The result of routing a URL
#[allow(missing_debug_implementations)]
pub struct Selection<'a, State> {
pub(crate) struct Selection<'a, State> {
pub(crate) endpoint: &'a DynEndpoint<State>,
pub(crate) params: Params,
}

impl<State: Clone + Send + Sync + 'static> Router<State> {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Router {
method_map: HashMap::default(),
all_method_router: MethodRouter::new(),
}
}

pub fn add(&mut self, path: &str, method: http_types::Method, ep: Box<DynEndpoint<State>>) {
pub(crate) fn add(
&mut self,
path: &str,
method: http_types::Method,
ep: Box<DynEndpoint<State>>,
) {
self.method_map
.entry(method)
.or_insert_with(MethodRouter::new)
.add(path, ep)
}

pub fn add_all(&mut self, path: &str, ep: Box<DynEndpoint<State>>) {
pub(crate) fn add_all(&mut self, path: &str, ep: Box<DynEndpoint<State>>) {
self.all_method_router.add(path, ep)
}

pub fn route(&self, path: &str, method: http_types::Method) -> Selection<'_, State> {
pub(crate) fn route(&self, path: &str, method: http_types::Method) -> Selection<'_, State> {
if let Some(Match { handler, params }) = self
.method_map
.get(&method)
Expand Down
6 changes: 3 additions & 3 deletions src/security/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub struct CorsMiddleware {
max_age: HeaderValue,
}

pub const DEFAULT_MAX_AGE: &str = "86400";
pub const DEFAULT_METHODS: &str = "GET, POST, OPTIONS";
pub const WILDCARD: &str = "*";
pub(crate) const DEFAULT_MAX_AGE: &str = "86400";
pub(crate) const DEFAULT_METHODS: &str = "GET, POST, OPTIONS";
pub(crate) const WILDCARD: &str = "*";

impl CorsMiddleware {
/// Creates a new Cors middleware.
Expand Down
5 changes: 3 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ impl<State: Clone + Send + Sync + 'static> Server<State> {
/// #
/// # Ok(()) }
/// ```
pub async fn respond<R>(&self, req: impl Into<http_types::Request>) -> http_types::Result<R>
pub async fn respond<Req, Res>(&self, req: Req) -> http_types::Result<Res>
where
R: From<http_types::Response>,
Req: Into<http_types::Request>,
Res: From<http_types::Response>,
{
let req = req.into();
let Self {
Expand Down