Skip to main content

CookieStore

Struct CookieStore 

Source
pub struct CookieStore<T, J> { /* private fields */ }
Expand description

A typed value backed by a single cookie.

Holds a value of type T in memory. commit serializes it as JSON and writes it through the configured Cookies jar.

Reads and mutations affect only the in-memory value until you commit. Dropping the store or calling rollback discards those changes. Call remove to delete the cookie instead.

Obtain one by reading the incoming cookie through cookie_store:

// src/app/api/cart.rs
use serde::{Deserialize, Serialize};
use topcoat::{
    Result,
    context::Cx,
    cookie::{cookie_store, private_cookies},
    router::route,
};

#[derive(Default, Serialize, Deserialize)]
struct Cart {
    items: Vec<String>,
}

#[route(POST)]
async fn add_item(cx: &Cx) -> Result<String> {
    // `commit` writes the cookie and hands the value back; without it the
    // change is discarded.
    let cart = cookie_store::<Cart, _>(private_cookies(cx), "cart")
        .parse_or_default()
        .update(|cart| cart.items.push("widget".to_owned()))
        .commit()?;

    Ok(format!("{} items in cart", cart.items.len()))
}

Implementations§

Source§

impl<T, J> CookieStore<T, J>

Source

pub fn new(jar: J, key: impl Into<Cow<'static, str>>, value: T) -> Self

Builds a store around an already-known value.

Most code instead goes through cookie_store and one of the parse* methods, which read the existing cookie first.

Source

pub fn read(&self) -> &T

Returns a reference to the in-memory value.

Source

pub fn get(&self) -> T
where T: Clone,

Returns a clone of the in-memory value.

Source

pub fn set(self, value: T) -> Self

Replaces the in-memory value, returning the store so calls can be chained.

Like every mutation, this is not persisted until commit.

Source

pub fn update<F>(self, f: F) -> Self
where F: FnOnce(&mut T),

Mutates the in-memory value in place, returning the store so calls can be chained.

Like every mutation, this is not persisted until commit.

let cart = cookie_store::<Cart, _>(private_cookies(cx), "cart")
    .parse_or_default()
    .update(|cart| cart.items.push("widget".to_owned()))
    .commit()?;
Source

pub fn commit(self) -> Result<T, Error>

Serializes the current value, queues it on the backing jar as a Set-Cookie, and returns the value.

Call this after any work that must succeed before saving the value.

§Errors

Returns Err if serializing T to JSON fails.

Source

pub fn rollback(self)

Discards the store along with any uncommitted changes.

Equivalent to dropping the store without calling commit; it exists to make that intent explicit at the call site.

Source

pub fn remove(self)

Queues a removal of the backing cookie, expiring it on the client.

Drops the in-memory value and removes the cookie through the backing jar. Use the same jar configuration as when writing so the browser can match the cookie’s name, path, and domain.

Auto Trait Implementations§

§

impl<T, J> Freeze for CookieStore<T, J>
where J: Freeze, T: Freeze,

§

impl<T, J> RefUnwindSafe for CookieStore<T, J>

§

impl<T, J> Send for CookieStore<T, J>
where J: Send, T: Send,

§

impl<T, J> Sync for CookieStore<T, J>
where J: Sync, T: Sync,

§

impl<T, J> Unpin for CookieStore<T, J>
where J: Unpin, T: Unpin,

§

impl<T, J> UnsafeUnpin for CookieStore<T, J>
where J: UnsafeUnpin, T: UnsafeUnpin,

§

impl<T, J> UnwindSafe for CookieStore<T, J>
where J: UnwindSafe, T: UnwindSafe,

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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