pub struct MultiSerializer<PrimarySerializer> { /* private fields */ }
Expand description

The MultiSerializer provides the ability to sign values with a given serializer, but also try a series of fallback serializers. This is useful if you are rotating keys, and want to sign things using a new key, but allow an old serializer to unsign values.

Exmaple

use itsdangerous::*;

let primary = serializer_with_signer(default_builder("new key").build(), URLSafeEncoding);
let fallback = serializer_with_signer(default_builder("old key").build(), URLSafeEncoding);

let signed_with_new_key = primary.sign(&"Signed with new key".to_owned()).unwrap();
let signed_with_old_key = fallback.sign(&"Signed with old key".to_owned()).unwrap();

let multi = MultiSerializer::new(primary).add_fallback(fallback);

assert_eq!(multi.unsign::<String>(&signed_with_new_key).unwrap(), "Signed with new key");
assert_eq!(multi.unsign::<String>(&signed_with_old_key).unwrap(), "Signed with old key");

Implementations

Constructs a new MultiSerializer with a given Serializer as the primary serializer. The primary serializer is the one that will be used to sign values, and the first serializer that will be attempted while trying to unsign.

Remarks

If the primary serializer is used to unsign a value, no dynamic dispatch takes place. That is to say, the MultiSerializer is its fastest when only the primary serializer is required to unsign a value, and when signing a value, it is a zero-cost abstraction.

Adds a Serializer to as a fallback, that will be attempted to be used to unsign a value if the primary serializer fails to unsign a value.

Remarks

Fallback serializers are attempted in the order they are added. For optimal performance when using fallbacks, add them in the order they will probably be used. Meaning, if you have a 2 fallbacks, consider adding the one you expect to be sucecessful before the other.

Trait Implementations

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.