Implemented the empty array case

This commit is contained in:
Cerber Ursi
2021-11-27 19:10:29 +07:00
parent 23a7b089e4
commit 2c27da0a80
3 changed files with 44 additions and 1 deletions
+19 -1
View File
@@ -1,7 +1,25 @@
use crate::ser::SerResult;
use std::io::Write;
pub(crate) fn tuple_converter(mut output: impl Write, len: usize) -> SerResult {
pub(crate) fn tuple_converter(output: impl Write, len: usize) -> SerResult {
if len > 0 {
non_zero_size(output, len)
} else {
zero_size(output)
}
}
fn zero_size(mut output: impl Write) -> SerResult {
write!(output, "
#[inline]
fn convert<T>(_: ()) -> [T; 0] {{
[]
}}
")?;
Ok(())
}
fn non_zero_size(mut output: impl Write, len: usize) -> SerResult {
write!(
output,
"
+15
View File
@@ -174,6 +174,21 @@
//! assert_eq!(arr, [1, 2, 3, 4]);
//! ```
//!
//! #### Zero-sized arrays
//! Since the code presented above would work only for non-empty tuples, we have to handle the "empty tuple" case
//! differently. Fortunately for us, the "real" empty tuple is handled as a unit, so we can directly emit the code
//! which yields an empty array:
//! ```
//! let arr: [i32; 0] = {
//! #[inline]
//! fn convert<T>(_: ()) -> [T; 0] {
//! []
//! }
//!
//! convert(())
//! };
//! ```
//!
//! ### Maps
//!
//! Since Rust doesn't have the notion of map literals, we can't construct one directly. However, standard map-like
+10
View File
@@ -118,4 +118,14 @@ pub struct Container(pub [u8; 2]);
"""
value = """
definition::Container([1, 2])
"""
[empty_array]
main_type = "ZeroSizeContainer"
definition = """
#[derive(PartialEq, Debug, Serialize)]
pub struct ZeroSizeContainer(pub [u8; 0]);
"""
value = """
definition::ZeroSizeContainer([])
"""