pub struct ResponseCookies<'c> { /* private fields */ }
Expand description

A collection of ResponseCookies to be attached to an HTTP response using the Set-Cookie header.

A set’s life begins via ResponseCookies::new() and calls to ResponseCookies::insert():

use biscotti::{ResponseCookie, ResponseCookies};

let mut set = ResponseCookies::new();
set.insert(("name", "value"));
set.insert(("second", "another"));
set.insert(ResponseCookie::new("third", "again").set_path("/"));

If you want to tell the client to remove a cookie, you need to insert a RemovalCookie into the set.
Note that any T: Into<ResponseCookie> can be passed into these methods.

use biscotti::{ResponseCookie, ResponseCookies, RemovalCookie, ResponseCookieId};

let mut set = ResponseCookies::new();
let removal = RemovalCookie::new("name").set_path("/");
// This will tell the client to remove the cookie with name "name"
// and path "/".
set.insert(removal);

// If you insert a cookie with the same name and path, it will replace
// the removal cookie.
let cookie = ResponseCookie::new("name", "value").set_path("/");
set.insert(cookie);

let retrieved = set.get(ResponseCookieId::new("name").set_path("/")).unwrap();
assert_eq!(retrieved.value(), "value");

If you want to remove a cookie from the set without telling the client to remove it, you can use ResponseCookies::discard().

Cookies can be retrieved with ResponseCookies::get().
Check the method’s documentation for more information.

Implementations§

source§

impl<'c> ResponseCookies<'c>

source

pub fn new() -> ResponseCookies<'c>

Creates an empty cookie set.

§Example
use biscotti::ResponseCookies;

let set = ResponseCookies::new();
assert_eq!(set.iter().count(), 0);
source

pub fn get<I: Into<ResponseCookieId<'c>>>( &self, id: I ) -> Option<&ResponseCookie<'c>>

Returns a reference to the ResponseCookie inside this set with the specified id.

§Via id
use biscotti::{ResponseCookies, ResponseCookie, ResponseCookieId};

let mut set = ResponseCookies::new();
assert!(set.get("name").is_none());

let cookie = ResponseCookie::new("name", "value").set_path("/");
set.insert(cookie);

// By specifying just the name, the domain and path are assumed to be None.
let id = ResponseCookieId::new("name");
// `name` has a path of `/`, so it doesn't match the empty path.
assert!(set.get(id).is_none());

let id = ResponseCookieId::new("name").set_path("/");
// You need to specify a matching path to get the cookie we inserted above.
assert_eq!(set.get(id).map(|c| c.value()), Some("value"));
§Via name
use biscotti::{ResponseCookies, ResponseCookie, ResponseCookieId};

let mut set = ResponseCookies::new();
assert!(set.get("name").is_none());

let cookie = ResponseCookie::new("name", "value");
set.insert(cookie);

// By specifying just the name, the domain and path are assumed to be None.
assert_eq!(set.get("name").map(|c| c.value()), Some("value"));
source

pub fn insert<C: Into<ResponseCookie<'c>>>( &mut self, cookie: C ) -> Option<ResponseCookie<'c>>

Inserts cookie into this set.
If a cookie with the same ResponseCookieId already exists, it is replaced with cookie and the old cookie is returned.

§Example
use biscotti::{ResponseCookies, ResponseCookie, ResponseCookieId};

let mut set = ResponseCookies::new();
set.insert(("name", "value"));
set.insert(("second", "two"));
// Replaces the "second" cookie with a new one.
assert!(set.insert(("second", "three")).is_some());

assert_eq!(set.get("name").map(|c| c.value()), Some("value"));
assert_eq!(set.get("second").map(|c| c.value()), Some("three"));
assert_eq!(set.iter().count(), 2);

// If we insert another cookie with name "second", but different domain and path,
// it won't replace the existing one.
let cookie = ResponseCookie::new("second", "four").set_domain("rust-lang.org");
set.insert(cookie);

assert_eq!(set.get("second").map(|c| c.value()), Some("three"));
let id = ResponseCookieId::new("second").set_domain("rust-lang.org");
assert_eq!(set.get(id).map(|c| c.value()), Some("four"));
assert_eq!(set.iter().count(), 3);
source

pub fn discard<I: Into<ResponseCookieId<'c>>>(&mut self, id: I)

Discard cookie from this set.

discard does not instruct the client to remove the cookie.
You need to insert a RemovalCookie into ResponseCookies to do that.

§Example
use biscotti::{ResponseCookies, ResponseCookie};

let mut set = ResponseCookies::new();
set.insert(("second", "two"));
set.discard("second");

assert!(set.get("second").is_none());
assert_eq!(set.iter().count(), 0);
§Example with path and domain

A cookie is identified by its name, domain, and path. If you want to discard a cookie with a non-empty domain and/or path, you need to specify them.

use biscotti::{ResponseCookies, ResponseCookie, ResponseCookieId};

let mut set = ResponseCookies::new();
let cookie = ResponseCookie::new("name", "value").set_domain("rust-lang.org").set_path("/");
let id = cookie.id();
set.insert((cookie));

// This won't discard the cookie because the path and the domain don't match.
set.discard("second");
assert_eq!(set.iter().count(), 1);
assert!(set.get(id).is_some());

// This will discard the cookie because the name, the path and the domain match.
let id = ResponseCookieId::new("name").set_domain("rust-lang.org").set_path("/");
set.discard(id);
assert_eq!(set.iter().count(), 0);
source

pub fn iter(&self) -> Iter<'_, '_>

Returns an iterator over all the cookies present in this set.

§Example
use biscotti::{ResponseCookies, ResponseCookie};

let mut set = ResponseCookies::new();

set.insert(("name", "value"));
set.insert(("second", "two"));
set.insert(("new", "third"));
set.insert(("another", "fourth"));
set.insert(("yac", "fifth"));

set.discard("name");
set.discard("another");

// There are three cookies in the set: "second", "new", and "yac".
for cookie in set.iter() {
    match cookie.name() {
        "second" => assert_eq!(cookie.value(), "two"),
        "new" => assert_eq!(cookie.value(), "third"),
        "yac" => assert_eq!(cookie.value(), "fifth"),
        _ => unreachable!("there are only three cookies in the set")
    }
}
source§

impl<'a, 'c: 'a> ResponseCookies<'c>

source

pub fn header_values( self, processor: &'a Processor ) -> impl Iterator<Item = String> + 'a

Returns the values that should be sent to the client as Set-Cookie headers.

Trait Implementations§

source§

impl<'c> Clone for ResponseCookies<'c>

source§

fn clone(&self) -> ResponseCookies<'c>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'c> Debug for ResponseCookies<'c>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'c> Default for ResponseCookies<'c>

source§

fn default() -> ResponseCookies<'c>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'c> RefUnwindSafe for ResponseCookies<'c>

§

impl<'c> Send for ResponseCookies<'c>

§

impl<'c> Sync for ResponseCookies<'c>

§

impl<'c> Unpin for ResponseCookies<'c>

§

impl<'c> UnwindSafe for ResponseCookies<'c>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V