[][src]Function serde_test::assert_ser_tokens_error

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

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

This code runs with edition 2018
use std::sync::{Arc, Mutex};
use std::thread;

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

#[derive(Serialize)]
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.
        panic!()
    }).join();

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