Struct itsdangerous::MultiSerializer
source · [−]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
sourceimpl<PrimarySerializer> MultiSerializer<PrimarySerializer> where
PrimarySerializer: Serializer,
impl<PrimarySerializer> MultiSerializer<PrimarySerializer> where
PrimarySerializer: Serializer,
sourcepub fn new(primary_serializer: PrimarySerializer) -> Self
pub fn new(primary_serializer: PrimarySerializer) -> Self
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.
sourcepub fn add_fallback<FallbackSerializer>(
self,
fallback_serializer: FallbackSerializer
) -> Self where
FallbackSerializer: UnsignToString + 'static,
pub fn add_fallback<FallbackSerializer>(
self,
fallback_serializer: FallbackSerializer
) -> Self where
FallbackSerializer: UnsignToString + 'static,
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
sourceimpl<PrimarySerializer> Serializer for MultiSerializer<PrimarySerializer> where
PrimarySerializer: Serializer,
impl<PrimarySerializer> Serializer for MultiSerializer<PrimarySerializer> where
PrimarySerializer: Serializer,
Auto Trait Implementations
impl<PrimarySerializer> !RefUnwindSafe for MultiSerializer<PrimarySerializer>
impl<PrimarySerializer> !Send for MultiSerializer<PrimarySerializer>
impl<PrimarySerializer> !Sync for MultiSerializer<PrimarySerializer>
impl<PrimarySerializer> Unpin for MultiSerializer<PrimarySerializer> where
PrimarySerializer: Unpin,
impl<PrimarySerializer> !UnwindSafe for MultiSerializer<PrimarySerializer>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more