Struct cookie::CookieBuilder
source · pub struct CookieBuilder<'c> { /* private fields */ }
Expand description
Structure that follows the builder pattern for building Cookie
structs.
To construct a cookie:
- Call
Cookie::build
to start building. - Use any of the builder methods to set fields in the cookie.
- Call
CookieBuilder::finish()
to retrieve the built cookie.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let cookie: Cookie = Cookie::build("name", "value")
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
.max_age(Duration::days(1))
.finish();
Implementations§
source§impl<'c> CookieBuilder<'c>
impl<'c> CookieBuilder<'c>
sourcepub fn new<N, V>(name: N, value: V) -> Self
pub fn new<N, V>(name: N, value: V) -> Self
Creates a new CookieBuilder
instance from the given name and value.
This method is typically called indirectly via Cookie::build()
.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar").finish();
assert_eq!(c.name_value(), ("foo", "bar"));
sourcepub fn expires<E: Into<Expiration>>(self, when: E) -> Self
pub fn expires<E: Into<Expiration>>(self, when: E) -> Self
Sets the expires
field in the cookie being built.
§Example
use cookie::{Cookie, Expiration};
use cookie::time::OffsetDateTime;
let c = Cookie::build("foo", "bar")
.expires(OffsetDateTime::now_utc())
.finish();
assert!(c.expires().is_some());
let c = Cookie::build("foo", "bar")
.expires(None)
.finish();
assert_eq!(c.expires(), Some(Expiration::Session));
sourcepub fn max_age(self, value: Duration) -> Self
pub fn max_age(self, value: Duration) -> Self
Sets the max_age
field in the cookie being built.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let c = Cookie::build("foo", "bar")
.max_age(Duration::minutes(30))
.finish();
assert_eq!(c.max_age(), Some(Duration::seconds(30 * 60)));
sourcepub fn domain<D: Into<Cow<'c, str>>>(self, value: D) -> Self
pub fn domain<D: Into<Cow<'c, str>>>(self, value: D) -> Self
Sets the domain
field in the cookie being built.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar")
.domain("www.rust-lang.org")
.finish();
assert_eq!(c.domain(), Some("www.rust-lang.org"));
sourcepub fn path<P: Into<Cow<'c, str>>>(self, path: P) -> Self
pub fn path<P: Into<Cow<'c, str>>>(self, path: P) -> Self
Sets the path
field in the cookie being built.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar")
.path("/")
.finish();
assert_eq!(c.path(), Some("/"));
sourcepub fn secure(self, value: bool) -> Self
pub fn secure(self, value: bool) -> Self
Sets the secure
field in the cookie being built.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar")
.secure(true)
.finish();
assert_eq!(c.secure(), Some(true));
sourcepub fn http_only(self, value: bool) -> Self
pub fn http_only(self, value: bool) -> Self
Sets the http_only
field in the cookie being built.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar")
.http_only(true)
.finish();
assert_eq!(c.http_only(), Some(true));
sourcepub fn same_site(self, value: SameSite) -> Self
pub fn same_site(self, value: SameSite) -> Self
Sets the same_site
field in the cookie being built.
§Example
use cookie::{Cookie, SameSite};
let c = Cookie::build("foo", "bar")
.same_site(SameSite::Strict)
.finish();
assert_eq!(c.same_site(), Some(SameSite::Strict));
sourcepub fn permanent(self) -> Self
pub fn permanent(self) -> Self
Makes the cookie being built ‘permanent’ by extending its expiration and max age 20 years into the future.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let c = Cookie::build("foo", "bar")
.permanent()
.finish();
assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
sourcepub fn finish(self) -> Cookie<'c>
pub fn finish(self) -> Cookie<'c>
Finishes building and returns the built Cookie
.
§Example
use cookie::Cookie;
let c = Cookie::build("foo", "bar")
.domain("crates.io")
.path("/")
.finish();
assert_eq!(c.name_value(), ("foo", "bar"));
assert_eq!(c.domain(), Some("crates.io"));
assert_eq!(c.path(), Some("/"));
Trait Implementations§
source§impl<'c> Clone for CookieBuilder<'c>
impl<'c> Clone for CookieBuilder<'c>
source§fn clone(&self) -> CookieBuilder<'c>
fn clone(&self) -> CookieBuilder<'c>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<'c> Freeze for CookieBuilder<'c>
impl<'c> RefUnwindSafe for CookieBuilder<'c>
impl<'c> Send for CookieBuilder<'c>
impl<'c> Sync for CookieBuilder<'c>
impl<'c> Unpin for CookieBuilder<'c>
impl<'c> UnwindSafe for CookieBuilder<'c>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)