Struct rustful::context::Parameters [] [src]

pub struct Parameters(_);

An extended HashMap with extra functionality for value parsing.

Some of the methods from HashMap has been wrapped to provide a more ergonomic API, where anything that can be represented as a byte slice can be used as a key.

Methods

impl Parameters
[src]

Create an empty Parameters.

Get a parameter as a UTF-8 string. A lossy conversion will be performed if it's not encoded as UTF-8. Use get_raw to get the original data.

Get a parameter that may or may not be a UTF-8 string.

Get a mutable parameter that may or may not be a UTF-8 string.

Returns true if a parameter with the given key exists.

Insert a parameter.

Remove a parameter and return it.

Gets the given key's corresponding parameter in the map for in-place manipulation.

Try to parse an entry as T, if it exists. The error will be None if the entry does not exist, and Some if it does exists, but the parsing failed.

fn my_handler(context: Context, response: Response) {
    let age: Result<u8, _> = context.variables.parse("age");
    match age {
        Ok(age) => response.send(format!("age: {}", age)),
        Err(Some(_)) => response.send("age must be a positive number"),
        Err(None) => response.send("no age provided")
    }
}

Try to parse an entry as T, if it exists, or return the default in or.

fn my_handler(context: Context, response: Response) {
    let page = context.variables.parse_or("page", 0u8);
    response.send(format!("current page: {}", page));
}

Try to parse an entry as T, if it exists, or create a new one using or_else. The or_else function will receive the parsing error if the value existed, but was impossible to parse.

fn my_handler(context: Context, response: Response) {
    let science = context.variables.parse_or_else("science", |_| do_heavy_stuff());
    response.send(format!("science value: {}", science));
}

Methods from Deref<Target = HashMap<MaybeUtf8Owned, MaybeUtf8Owned>>

Important traits for &'a mut R

Returns a reference to the map's BuildHasher.

Examples

use std::collections::HashMap;
use std::collections::hash_map::RandomState;

let hasher = RandomState::new();
let map: HashMap<i32, i32> = HashMap::with_hasher(hasher);
let hasher: &RandomState = map.hasher();

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the HashMap<K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

Examples

use std::collections::HashMap;
let map: HashMap<i32, i32> = HashMap::with_capacity(100);
assert!(map.capacity() >= 100);

Reserves capacity for at least additional more elements to be inserted in the HashMap. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new allocation size overflows usize.

Examples

use std::collections::HashMap;
let mut map: HashMap<&str, i32> = HashMap::new();
map.reserve(10);

🔬 This is a nightly-only experimental API. (try_reserve)

new API

Tries to reserve capacity for at least additional more elements to be inserted in the given HashMap<K,V>. The collection may reserve more space to avoid frequent reallocations.

Errors

If the capacity overflows, or the allocator reports a failure, then an error is returned.

Examples

#![feature(try_reserve)]
use std::collections::HashMap;
let mut map: HashMap<&str, isize> = HashMap::new();
map.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

Examples

use std::collections::HashMap;

let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);

🔬 This is a nightly-only experimental API. (shrink_to)

new API

Shrinks the capacity of the map with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

Panics if the current capacity is smaller than the supplied minimum capacity.

Examples

#![feature(shrink_to)]
use std::collections::HashMap;

let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10);
map.shrink_to(0);
assert!(map.capacity() >= 2);

Important traits for Keys<'a, K, V>

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for key in map.keys() {
    println!("{}", key);
}

Important traits for Values<'a, K, V>

An iterator visiting all values in arbitrary order. The iterator element type is &'a V.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values() {
    println!("{}", val);
}

Important traits for ValuesMut<'a, K, V>

An iterator visiting all values mutably in arbitrary order. The iterator element type is &'a mut V.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values_mut() {
    *val = *val + 10;
}

for val in map.values() {
    println!("{}", val);
}

Important traits for Iter<'a, K, V>

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a V).

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}

Important traits for IterMut<'a, K, V>

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

for (key, val) in &map {
    println!("key: {} val: {}", key, val);
}

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

use std::collections::HashMap;

let mut letters = HashMap::new();

for ch in "a short treatise on fungi".chars() {
    let counter = letters.entry(ch).or_insert(0);
    *counter += 1;
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);

Returns the number of elements in the map.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);

Returns true if the map contains no elements.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());

Important traits for Drain<'a, K, V>

Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
a.insert(1, "a");
a.insert(2, "b");

for (k, v) in a.drain().take(1) {
    assert!(k == 1 || k == 2);
    assert!(v == "a" || v == "b");
}

assert!(a.is_empty());

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

🔬 This is a nightly-only experimental API. (map_get_key_value)

Returns the key-value pair corresponding to the supplied key.

The supplied key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

#![feature(map_get_key_value)]
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");

Removes a key from the map, returning the value at the key if the key was previously in the map.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

🔬 This is a nightly-only experimental API. (hash_map_remove_entry)

Removes a key from the map, returning the stored key and value if the key was previously in the map.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

#![feature(hash_map_remove_entry)]
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove(&1), None);

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k,&mut v) returns false.

Examples

use std::collections::HashMap;

let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);

Trait Implementations

impl Clone for Parameters
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Deref for Parameters
[src]

The resulting type after dereferencing.

Dereferences the value.

impl DerefMut for Parameters
[src]

Mutably dereferences the value.

impl AsRef<HashMap<MaybeUtf8Owned, MaybeUtf8Owned>> for Parameters
[src]

Performs the conversion.

impl AsMut<HashMap<MaybeUtf8Owned, MaybeUtf8Owned>> for Parameters
[src]

Performs the conversion.

impl Into<HashMap<MaybeUtf8Owned, MaybeUtf8Owned>> for Parameters
[src]

Performs the conversion.

impl From<HashMap<MaybeUtf8Owned, MaybeUtf8Owned>> for Parameters
[src]

Performs the conversion.

impl PartialEq for Parameters
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Parameters
[src]

impl Debug for Parameters
[src]

Formats the value using the given formatter. Read more

impl Default for Parameters
[src]

Returns the "default value" for a type. Read more

impl IntoIterator for Parameters
[src]

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

impl<'a> IntoIterator for &'a Parameters
[src]

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

impl<'a> IntoIterator for &'a mut Parameters
[src]

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

impl<K: Into<MaybeUtf8Owned>, V: Into<MaybeUtf8Owned>> FromIterator<(K, V)> for Parameters
[src]

Creates a value from an iterator. Read more

impl<K: Into<MaybeUtf8Owned>, V: Into<MaybeUtf8Owned>> Extend<(K, V)> for Parameters
[src]

Extends a collection with the contents of an iterator. Read more

Auto Trait Implementations

impl Send for Parameters

impl Sync for Parameters