Skip to main content

Form

Struct Form 

Source
pub struct Form { /* private fields */ }
Available on crate feature alloc only.
Expand description

The owned result of a urlencoded parse: every field, in input order, duplicates kept.

Every decoded name and value lives in one buffer allocated once, and one table of positions; neither reallocates. Lookups are linear scans (there is no hash map, so hash flooding cannot apply; max_fields bounds every scan). On drop the buffer is wiped. Debug prints names and value lengths, never values. There is no Clone: a clone would be a second copy of every secret.

§Examples

let form = form_net::parse_urlencoded(b"role=user&role=admin&tag=red&tag=blue")?;
assert_eq!(form.get("role"), Some("user"));                 // first wins
assert!(form.get_one("role").is_err());                     // strict
assert_eq!(form.get_one("missing")?, None);
assert_eq!(form.get_all("tag").collect::<Vec<_>>(), ["red", "blue"]);
assert_eq!(form.iter().count(), 4);
assert_eq!(
    format!("{form:?}"),
    "Form { role: <4 bytes>, role: <5 bytes>, tag: <3 bytes>, tag: <4 bytes> }"
);

Implementations§

Source§

impl Form

Source

pub fn get(&self, name: &str) -> Option<&str>

The first value given for name, as browsers’ URLSearchParams.get returns. Use get_one where a repeated name must be refused.

§Examples
let form = form_net::parse_urlencoded(b"email=a%40b.example")?;
assert_eq!(form.get("email"), Some("a@b.example"));
assert_eq!(form.get("other"), None);
Source

pub fn get_one(&self, name: &str) -> Result<Option<&str>, FormError>

The value given for name, refusing a name given more than once (the parameter pollution defence).

§Errors

Extract { reason: Duplicate, field: None }.

§Examples
let form = form_net::parse_urlencoded(b"a=1&b=2&b=3")?;
assert_eq!(form.get_one("a")?, Some("1"));
assert!(form.get_one("b").is_err());
Source

pub fn get_all<'f>( &'f self, name: &'f str, ) -> impl Iterator<Item = &'f str> + 'f

Every value given for name, in input order.

§Examples
let form = form_net::parse_urlencoded(b"t=a&x=1&t=b")?;
assert_eq!(form.get_all("t").collect::<Vec<_>>(), ["a", "b"]);
Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &str)>

Every (name, value), in input order.

§Examples
let form = form_net::parse_urlencoded(b"a=1&b=2")?;
assert_eq!(form.iter().collect::<Vec<_>>(), [("a", "1"), ("b", "2")]);
Source

pub fn names(&self) -> impl Iterator<Item = &str>

Every name, in input order, repeats included.

§Examples
let form = form_net::parse_urlencoded(b"a=1&b=2&a=3")?;
assert_eq!(form.names().collect::<Vec<_>>(), ["a", "b", "a"]);
Source

pub fn len(&self) -> usize

The number of fields, repeats included.

§Examples
assert_eq!(form_net::parse_urlencoded(b"a&&b")?.len(), 2);
Source

pub fn is_empty(&self) -> bool

Whether there are no fields.

§Examples
assert!(form_net::parse_urlencoded(b"&&")?.is_empty());
Source

pub fn contains(&self, name: &str) -> bool

Whether name was given at least once.

§Examples
let form = form_net::parse_urlencoded(b"remember")?;
assert!(form.contains("remember"));

Trait Implementations§

Source§

impl Debug for Form

Source§

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

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

impl Drop for Form

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl Freeze for Form

§

impl RefUnwindSafe for Form

§

impl Send for Form

§

impl Sync for Form

§

impl Unpin for Form

§

impl UnsafeUnpin for Form

§

impl UnwindSafe for Form

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, 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.