| 1 | #![cfg (feature = "serde" )] |
| 2 | #![warn (rust_2018_idioms)] |
| 3 | |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use serde_test::{assert_tokens, Token}; |
| 6 | use slab::Slab; |
| 7 | |
| 8 | #[derive(Debug, Serialize, Deserialize)] |
| 9 | #[serde(transparent)] |
| 10 | struct SlabPartialEq<T>(Slab<T>); |
| 11 | |
| 12 | impl<T: PartialEq> PartialEq for SlabPartialEq<T> { |
| 13 | fn eq(&self, other: &Self) -> bool { |
| 14 | self.0.len() == other.0.len() |
| 15 | && self |
| 16 | .0 |
| 17 | .iter() |
| 18 | .zip(other.0.iter()) |
| 19 | .all(|(this, other)| this.0 == other.0 && this.1 == other.1) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn test_serde_empty() { |
| 25 | let slab = Slab::<usize>::new(); |
| 26 | assert_tokens( |
| 27 | &SlabPartialEq(slab), |
| 28 | &[Token::Map { len: Some(0) }, Token::MapEnd], |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn test_serde() { |
| 34 | let vec = vec![(1, 2), (3, 4), (5, 6)]; |
| 35 | let slab: Slab<_> = vec.iter().cloned().collect(); |
| 36 | assert_tokens( |
| 37 | &SlabPartialEq(slab), |
| 38 | &[ |
| 39 | Token::Map { len: Some(3) }, |
| 40 | Token::U64(1), |
| 41 | Token::I32(2), |
| 42 | Token::U64(3), |
| 43 | Token::I32(4), |
| 44 | Token::U64(5), |
| 45 | Token::I32(6), |
| 46 | Token::MapEnd, |
| 47 | ], |
| 48 | ); |
| 49 | } |
| 50 | |