|
| 1 | +#[cfg(not(feature = "std"))] |
| 2 | +use core as std; |
| 3 | + |
| 4 | +use crate::{FixedBitSet, BYTES}; |
| 5 | +use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; |
| 6 | +use serde::ser::{Serialize, SerializeStruct, Serializer}; |
| 7 | +use std::{convert::TryFrom, fmt}; |
| 8 | + |
| 9 | +struct BitSetByteSerializer<'a>(&'a FixedBitSet); |
| 10 | + |
| 11 | +impl Serialize for FixedBitSet { |
| 12 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 13 | + where |
| 14 | + S: Serializer, |
| 15 | + { |
| 16 | + let mut struct_serializer = serializer.serialize_struct("FixedBitset", 2)?; |
| 17 | + struct_serializer.serialize_field("length", &(self.length as u64))?; |
| 18 | + struct_serializer.serialize_field("data", &BitSetByteSerializer(self))?; |
| 19 | + struct_serializer.end() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl<'a> Serialize for BitSetByteSerializer<'a> { |
| 24 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 25 | + where |
| 26 | + S: Serializer, |
| 27 | + { |
| 28 | + let len = self.0.data.len() * BYTES; |
| 29 | + // PERF: Figure out a way to do this without allocating. |
| 30 | + let mut temp = Vec::with_capacity(len); |
| 31 | + for block in &self.0.data { |
| 32 | + temp.extend(&block.to_le_bytes()); |
| 33 | + } |
| 34 | + serializer.serialize_bytes(&temp) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<'de> Deserialize<'de> for FixedBitSet { |
| 39 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 40 | + where |
| 41 | + D: Deserializer<'de>, |
| 42 | + { |
| 43 | + enum Field { |
| 44 | + Length, |
| 45 | + Data, |
| 46 | + } |
| 47 | + |
| 48 | + impl<'de> Deserialize<'de> for Field { |
| 49 | + fn deserialize<D>(deserializer: D) -> Result<Field, D::Error> |
| 50 | + where |
| 51 | + D: Deserializer<'de>, |
| 52 | + { |
| 53 | + struct FieldVisitor; |
| 54 | + |
| 55 | + impl<'de> Visitor<'de> for FieldVisitor { |
| 56 | + type Value = Field; |
| 57 | + |
| 58 | + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 59 | + formatter.write_str("`length` or `data`") |
| 60 | + } |
| 61 | + |
| 62 | + fn visit_str<E>(self, value: &str) -> Result<Field, E> |
| 63 | + where |
| 64 | + E: de::Error, |
| 65 | + { |
| 66 | + match value { |
| 67 | + "length" => Ok(Field::Length), |
| 68 | + "data" => Ok(Field::Data), |
| 69 | + _ => Err(de::Error::unknown_field(value, FIELDS)), |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + deserializer.deserialize_identifier(FieldVisitor) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + struct FixedBitSetVisitor; |
| 79 | + |
| 80 | + impl<'de> Visitor<'de> for FixedBitSetVisitor { |
| 81 | + type Value = FixedBitSet; |
| 82 | + |
| 83 | + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 84 | + formatter.write_str("struct Duration") |
| 85 | + } |
| 86 | + |
| 87 | + fn visit_seq<V>(self, mut seq: V) -> Result<FixedBitSet, V::Error> |
| 88 | + where |
| 89 | + V: SeqAccess<'de>, |
| 90 | + { |
| 91 | + let length = seq |
| 92 | + .next_element()? |
| 93 | + .ok_or_else(|| de::Error::invalid_length(0, &self))?; |
| 94 | + let data = seq |
| 95 | + .next_element()? |
| 96 | + .ok_or_else(|| de::Error::invalid_length(1, &self))?; |
| 97 | + Ok(FixedBitSet { length, data }) |
| 98 | + } |
| 99 | + |
| 100 | + fn visit_map<V>(self, mut map: V) -> Result<FixedBitSet, V::Error> |
| 101 | + where |
| 102 | + V: MapAccess<'de>, |
| 103 | + { |
| 104 | + let mut length = None; |
| 105 | + let mut temp: Option<&[u8]> = None; |
| 106 | + while let Some(key) = map.next_key()? { |
| 107 | + match key { |
| 108 | + Field::Length => { |
| 109 | + if length.is_some() { |
| 110 | + return Err(de::Error::duplicate_field("length")); |
| 111 | + } |
| 112 | + length = Some(map.next_value()?); |
| 113 | + } |
| 114 | + Field::Data => { |
| 115 | + if temp.is_some() { |
| 116 | + return Err(de::Error::duplicate_field("data")); |
| 117 | + } |
| 118 | + temp = Some(map.next_value()?); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + let length = length.ok_or_else(|| de::Error::missing_field("length"))?; |
| 123 | + let temp = temp.ok_or_else(|| de::Error::missing_field("data"))?; |
| 124 | + let block_len = length / BYTES + 1; |
| 125 | + let mut data = Vec::with_capacity(block_len); |
| 126 | + for chunk in temp.chunks(BYTES) { |
| 127 | + match <&[u8; BYTES]>::try_from(chunk) { |
| 128 | + Ok(bytes) => data.push(usize::from_le_bytes(*bytes)), |
| 129 | + Err(_) => { |
| 130 | + let mut bytes = [0u8; BYTES]; |
| 131 | + bytes[0..BYTES].copy_from_slice(chunk); |
| 132 | + data.push(usize::from_le_bytes(bytes)); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + Ok(FixedBitSet { length, data }) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + const FIELDS: &'static [&'static str] = &["length", "data"]; |
| 141 | + deserializer.deserialize_struct("Duration", FIELDS, FixedBitSetVisitor) |
| 142 | + } |
| 143 | +} |
0 commit comments