Skip to content

Commit

Permalink
Refactor nested module into crate root
Browse files Browse the repository at this point in the history
Nested and "flat" arrays are now supported identically in serialization.

Deserialization is still to-do.

refs #2
  • Loading branch information
Kromey committed May 11, 2021
1 parent 5218afa commit 93cbd5c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 134 deletions.
51 changes: 35 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,65 @@
//! # Ok::<(), serde_json::Error>(())
//! ```
//!
//! Even nested arrays are supported:
//!
//! ```
//! # use serde::{Serialize, Deserialize};
//! # use serde_json;
//! #[derive(Serialize, Debug, PartialEq, Eq)]
//! struct NestedArray {
//! #[serde(with = "serde_arrays")]
//! arr: [[u32; 64]; 64],
//! #[serde(with = "serde_arrays")]
//! vec: Vec<[u32; 96]>,
//! }
//! # let data = NestedArray{ arr: [[1; 64]; 64], vec: vec![[2; 96]; 37], };
//! # let json = serde_json::to_string(&data)?;
//! # //let de_data = serde_json::from_str(&json)?;
//! # //assert_eq!(data, de_data);
//! # Ok::<(), serde_json::Error>(())
//! ```
//!
//! # MSRV
//!
//! This library relies on the const generics feature introduced in Rust 1.51.0.
//!
//! # Relevant links
//!
//! * The [Serde issue](https://github.com/serde-rs/serde/issues/1937) for const generics support
//! * [serde-big-array](https://crates.io/crates/serde-big-array) is a similar crate, but it
//! depends on `unsafe` code (whether its use of such is safe or not is beyond this scope)
//! * [serde-big-array](https://crates.io/crates/serde-big-array) is a similar crate for large
//! arrays and const generic arrays
//! * [serde_with](https://crates.io/crates/serde_with/) is a much more flexible and powerful
//! crate, but with arguably more complex ergonomics
//!
//! [Serde]: https://serde.rs/
use serde::{
de::{self, Deserialize, Deserializer, SeqAccess, Visitor},
ser::{Serialize, SerializeTuple, Serializer},
ser::{Serialize, Serializer},
};
use std::{fmt, marker::PhantomData, mem::MaybeUninit};

pub mod nested;
#[doc(hidden)]
pub mod serializable;
mod wrapper;
pub use serializable::Serializable;

/// Serialize const generic or arbitrarily-large arrays
///
/// For any array up to length `usize::MAX`, this function will allow Serde to properly serialize
/// it, provided of course that the type `T` is itself serializable.
/// Types must implement the [`Serializable`] trait; while this requirement sharply limits how
/// composable the final result is, the simple ergonomics make up for it.
///
/// This implementation is adapted from the [Serde documentataion][serialize_map]
/// For greater flexibility see [`serde_with`][serde_with].
///
/// [serialize_map]: https://serde.rs/impl-serialize.html#serializing-a-sequence-or-map
pub fn serialize<S, T, const N: usize>(data: &[T; N], ser: S) -> Result<S::Ok, S::Error>
/// [serde_with]: https://crates.io/crates/serde_with/
pub fn serialize<A, S, T, const N: usize>(data: &A, ser: S) -> Result<S::Ok, S::Error>
where
A: Serializable<T, N>,
S: Serializer,
T: Serialize,
{
// 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 data {
s.serialize_element(item)?;
}
s.end()
data.serialize(ser)
}

/// A Serde Deserializer `Visitor` for [T; N] arrays
Expand Down
102 changes: 0 additions & 102 deletions src/nested.rs

This file was deleted.

79 changes: 79 additions & 0 deletions src/serializable.rs
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()
}
27 changes: 27 additions & 0 deletions src/wrapper.rs
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)
}
}
12 changes: 3 additions & 9 deletions tests/common/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@ use serde::Serialize;

#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct NestedArray<const N: usize> {
#[serde(with = "serde_arrays::nested")]
#[serde(with = "serde_arrays")]
pub arr: [[u32; N]; 2],
}

#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct GenericNestedArray<const N: usize, const M: usize> {
#[serde(with = "serde_arrays::nested")]
#[serde(with = "serde_arrays")]
pub arr: [[u32; N]; M],
}

#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct VecArray<const N: usize> {
#[serde(with = "serde_arrays::nested")]
#[serde(with = "serde_arrays")]
pub arr: Vec<[u32; N]>,
}

#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct FlatArray<const N: usize> {
#[serde(with = "serde_arrays::nested")]
pub arr: [u32; N],
}
7 changes: 0 additions & 7 deletions tests/serialize_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ fn serialize_nested_array() {
assert_eq!(json, &j_generic);
assert_eq!(json, &j_vecced);
}

#[test]
fn serialize_flat_as_nested() {
let flat = FlatArray { arr: [1; 3] };
let j_flat = serde_json::to_string(&flat).unwrap();
assert_eq!("{\"arr\":[1,1,1]}", &j_flat);
}

0 comments on commit 93cbd5c

Please sign in to comment.