-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor nested module into crate root
Nested and "flat" arrays are now supported identically in serialization. Deserialization is still to-do. refs #2
- Loading branch information
Showing
6 changed files
with
144 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 Travis Veazey | ||
// | ||
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or | ||
// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
// https://opensource.org/licenses/MIT>, at your option. This file may not be | ||
// copied, modified, or distributed except according to those terms. | ||
|
||
use crate::wrapper::ArrayWrap; | ||
use serde::ser::{Serialize, SerializeSeq, SerializeTuple, Serializer}; | ||
|
||
/// Trait for types serializable using `serde_arrays` | ||
/// | ||
/// In order to serialize data using this crate, the type needs to implement this trait. While this | ||
/// approach has limitations in what can be supported (namely it limits support to only those types | ||
/// this trait is explicitly implemented on), the trade off is a significant increase in ergonomics. | ||
/// | ||
/// If the greater flexibility lost by this approach is needed, see [`serde_with`][serde_with]. | ||
/// | ||
/// [serde_with]: https://crates.io/crates/serde_with/ | ||
pub trait Serializable<T: Serialize, const N: usize> { | ||
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer; | ||
} | ||
|
||
impl<T: Serialize, const N: usize, const M: usize> Serializable<T, N> for [[T; N]; M] { | ||
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
// Fixed-length structures, including arrays, are supported in Serde as tuples | ||
// See: https://serde.rs/impl-serialize.html#serializing-a-tuple | ||
let mut s = ser.serialize_tuple(N)?; | ||
for item in self { | ||
let wrapped = ArrayWrap::new(item); | ||
s.serialize_element(&wrapped)?; | ||
} | ||
s.end() | ||
} | ||
} | ||
|
||
impl<T: Serialize, const N: usize> Serializable<T, N> for Vec<[T; N]> { | ||
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut s = ser.serialize_seq(Some(self.len()))?; | ||
for item in self { | ||
let wrapped = ArrayWrap::new(item); | ||
s.serialize_element(&wrapped)?; | ||
} | ||
s.end() | ||
} | ||
} | ||
|
||
impl<T: Serialize, const N: usize> Serializable<T, N> for [T; N] { | ||
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serialize_as_tuple(self, ser) | ||
} | ||
} | ||
|
||
/// Serialize an array | ||
/// | ||
/// In Serde arrays (and other fixed-length structures) are supported as tuples | ||
fn serialize_as_tuple<S, T, const N: usize>(data: &[T; N], ser: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: Serialize, | ||
{ | ||
// See: https://serde.rs/impl-serialize.html#serializing-a-tuple | ||
let mut s = ser.serialize_tuple(N)?; | ||
for item in data { | ||
s.serialize_element(item)?; | ||
} | ||
s.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 Travis Veazey | ||
// | ||
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or | ||
// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
// https://opensource.org/licenses/MIT>, at your option. This file may not be | ||
// copied, modified, or distributed except according to those terms. | ||
|
||
use serde::ser::{Serialize, Serializer}; | ||
|
||
pub struct ArrayWrap<'a, T: Serialize, const N: usize> { | ||
inner: &'a [T; N], | ||
} | ||
|
||
impl<'a, T: Serialize, const N: usize> ArrayWrap<'a, T, N> { | ||
pub fn new(array: &'a [T; N]) -> ArrayWrap<'a, T, N> { | ||
ArrayWrap { inner: array } | ||
} | ||
} | ||
|
||
impl<'a, T: Serialize, const N: usize> Serialize for ArrayWrap<'a, T, N> { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
super::serialize(self.inner, serializer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters