Implemented the empty array case
This commit is contained in:
+19
-1
@@ -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
@@ -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
|
||||
|
||||
@@ -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([])
|
||||
"""
|
||||
Reference in New Issue
Block a user