Skip to content

Commit

Permalink
Start to deduplicate 'Client' code. Test for non-Sync, non-Send.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Jun 17, 2020
1 parent ee3df2b commit 69bfc68
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 85 deletions.
74 changes: 29 additions & 45 deletions core/lib/src/local/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,66 +31,33 @@ impl Client {
{
self.runtime.borrow_mut().block_on(fut)
}
}

/// Construct a new `Client` from an instance of `Rocket` with cookie
/// tracking.
///
/// # Cookie Tracking
///
/// By default, a `Client` propagates cookie changes made by responses to
/// previously dispatched requests. In other words, if a previously
/// dispatched request resulted in a response that adds a cookie, any future
/// requests will contain the new cookies. Similarly, cookies removed by a
/// response won't be propagated further.
///
/// This is typically the desired mode of operation for a `Client` as it
/// removes the burden of manually tracking cookies. Under some
/// circumstances, however, disabling this tracking may be desired. The
/// [`untracked()`](Client::untracked()) method creates a `Client` that
/// _will not_ track cookies.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `LaunchError` is returned.
///
impl_client!(Client,
/// # Example
///
/// ```rust
/// use rocket::local::blocking::Client;
///
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
/// ```
#[inline(always)]
pub fn new(rocket: Rocket) -> Result<Client, LaunchError> {
[] new(rocket) {
Self::_new(rocket, true)
}
},

/// Construct a new `Client` from an instance of `Rocket` _without_ cookie
/// tracking.
///
/// # Cookie Tracking
///
/// Unlike the [`new()`](Client::new()) constructor, a `Client` returned
/// from this method _does not_ automatically propagate cookie changes.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `LaunchError` is returned.
///
/// # Example
///
/// ```rust
/// use rocket::local::blocking::Client;
///
/// let client = Client::untracked(rocket::ignite()).expect("valid rocket");
/// ```
#[inline(always)]
pub fn untracked(rocket: Rocket) -> Result<Client, LaunchError> {
[] untracked(rocket) {
Self::_new(rocket, false)
}
},
);

impl Client {
/// Returns a reference to the `Manifest` of the `Rocket` this client is
/// creating requests for.
///
Expand Down Expand Up @@ -286,7 +253,24 @@ impl Client {
}
}

#[cfg(test)]
mod test {
// TODO: assert that client is !Sync - e.g. with static_assertions or another tool
}
/// ```no_run
/// // Just to ensure we get the path right in the next tests.
/// use rocket::local::blocking::Client;
/// ```
///
/// ```compile_fail
/// use rocket::local::blocking::Client;
///
/// fn not_sync<T: Sync>();
/// not_sync::<Client>();
/// ```
///
/// ```compile_fail
/// use rocket::local::blocking::Client;
///
/// fn not_send<T: Send>();
/// not_send::<Client>();
/// ```
#[cfg(doctest)]
#[allow(dead_code)]
fn test_not_sync_or_send() {}
49 changes: 9 additions & 40 deletions core/lib/src/local/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,9 @@ impl Client {

Ok(Client { manifest, cookies })
}
}

/// Construct a new `Client` from an instance of `Rocket` with cookie
/// tracking.
///
/// # Cookie Tracking
///
/// By default, a `Client` propagates cookie changes made by responses to
/// previously dispatched requests. In other words, if a previously
/// dispatched request resulted in a response that adds a cookie, any future
/// requests will contain the new cookies. Similarly, cookies removed by a
/// response won't be propagated further.
///
/// This is typically the desired mode of operation for a `Client` as it
/// removes the burden of manually tracking cookies. Under some
/// circumstances, however, disabling this tracking may be desired. The
/// [`untracked()`](Client::untracked()) method creates a `Client` that
/// _will not_ track cookies.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `LaunchError` is returned.
///
impl_client!(Client,
/// # Example
///
/// ```rust
Expand All @@ -122,23 +102,10 @@ impl Client {
/// # };
/// ```
#[inline(always)]
pub async fn new(rocket: Rocket) -> Result<Client, LaunchError> {
Client::_new(rocket, true).await
}
[ async ] new(rocket) {
Self::_new(rocket, true).await
},

/// Construct a new `Client` from an instance of `Rocket` _without_ cookie
/// tracking.
///
/// # Cookie Tracking
///
/// Unlike the [`new()`](Client::new()) constructor, a `Client` returned
/// from this method _does not_ automatically propagate cookie changes.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `LaunchError` is returned.
///
/// # Example
///
/// ```rust
Expand All @@ -149,10 +116,12 @@ impl Client {
/// # });
/// ```
#[inline(always)]
pub async fn untracked(rocket: Rocket) -> Result<Client, LaunchError> {
[ async ] untracked(rocket) {
Client::_new(rocket, false).await
}
},
);

impl Client {
/// Returns a reference to the `Manifest` of the `Rocket` this client is
/// creating requests for.
///
Expand Down
60 changes: 60 additions & 0 deletions core/lib/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,66 @@
//! [`Client`]: crate::local::Client
//! [`LocalRequest`]: crate::local::LocalRequest
macro_rules! impl_client {
(
$name:ident,

$(#[$($n_a:meta)*])* [$($n_p:tt)*] new($n_r:ident) $new:expr,
$(#[$($u_a:meta)*])* [$($u_p:tt)*] untracked($u_r:ident) $untracked:expr,

) =>
{
// use crate::rocket::{Rocket, Manifest};
// use crate::local::LocalRequest;
// use crate::http::{Method, private::CookieJar};
// use crate::error::LaunchError;

impl $name {
/// Construct a new `Client` from an instance of `Rocket` with cookie
/// tracking.
///
/// # Cookie Tracking
///
/// By default, a `Client` propagates cookie changes made by responses to
/// previously dispatched requests. In other words, if a previously
/// dispatched request resulted in a response that adds a cookie, any future
/// requests will contain the new cookies. Similarly, cookies removed by a
/// response won't be propagated further.
///
/// This is typically the desired mode of operation for a `Client` as it
/// removes the burden of manually tracking cookies. Under some
/// circumstances, however, disabling this tracking may be desired. The
/// [`untracked()`](Client::untracked()) method creates a `Client` that
/// _will not_ track cookies.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `LaunchError` is returned.
$(#[$($n_a)*])*
pub $($n_p)* fn new($n_r: Rocket) -> Result<Self, LaunchError> {
$new
}

/// Construct a new `Client` from an instance of `Rocket` _without_
/// cookie tracking.
///
/// # Cookie Tracking
///
/// Unlike the [`new()`](Client::new()) constructor, a `Client` returned
/// from this method _does not_ automatically propagate cookie changes.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network
/// errors, the `LaunchError` is returned.
$(#[$($u_a)*])*
pub $($u_p)* fn untracked($u_r: Rocket) -> Result<Self, LaunchError> {
$untracked
}
}
}}

pub mod blocking;
mod request;
mod client;
Expand Down

0 comments on commit 69bfc68

Please sign in to comment.