Function serde_test::assert_ser_tokens_error[][src]

pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str) where
    T: Serialize
Expand description

Asserts that value serializes to the given tokens, and then yields error.

use std::sync::{Arc, Mutex};
use std::thread;

use serde::Serialize;
use serde_test::{assert_ser_tokens_error, Token};

struct Example {
    lock: Arc<Mutex<u32>>,

fn main() {
    let example = Example { lock: Arc::new(Mutex::new(0)) };
    let lock = example.lock.clone();

    let _ = thread::spawn(move || {
        // This thread will acquire the mutex first, unwrapping the result
        // of `lock` because the lock has not been poisoned.
        let _guard = lock.lock().unwrap();

        // This panic while holding the lock (`_guard` is in scope) will
        // poison the mutex.

    let expected = &[
        Token::Struct { name: "Example", len: 1 },
    let error = "lock poison error while serializing";
    assert_ser_tokens_error(&example, expected, error);