Implemented arrays unevaling

This commit is contained in:
Cerber Ursi
2021-11-27 19:10:25 +07:00
parent 1e7307876a
commit abd8c2b341
5 changed files with 54 additions and 5 deletions
+32
View File
@@ -0,0 +1,32 @@
use std::{fs::File, path::PathBuf, io::Write};
fn main() {
let path: PathBuf = [std::env::var("OUT_DIR").unwrap(), "from_tuple.rs".into()].iter().collect();
let mut convert = File::create(path).expect("Failed to create file for tuples conversions");
for i in 1..=32 {
let array = format!("[T; {}]", i);
let tuple = format!("({})", (0..i).map(|_| "T,").collect::<String>());
let mapping = format!("[{}]", (0..i).map(|index| format!("tuple.{}", index)).collect::<Vec<_>>().join(","));
write!(convert, "
impl<T> FromTuple<{tuple}> for {array} {{
#[inline]
fn from_tuple(tuple: {tuple}) -> Self {{
{mapping}
}}
}}
", array = array, tuple = tuple, mapping = mapping).expect("Failed to write the conversion code");
let types = (0..i).map(|index| format!("T{}", index)).collect::<Vec<_>>().join(",");
write!(convert, "
impl<{types}> FromTuple<({types},)> for ({types},) {{
#[inline]
fn from_tuple(tuple: ({types},)) -> Self {{
tuple
}}
}}
#[inline]
pub fn convert_tuple_{count}<{types}, Out: FromTuple<({types},)>>(tuple: ({types},)) -> Out {{
Out::from_tuple(tuple)
}}
", types = types, count = i).expect("Failed to write the conversion code");
}
}
+5
View File
@@ -0,0 +1,5 @@
pub trait FromTuple<T> {
fn from_tuple(tuple: T) -> Self;
}
include!(concat!(env!("OUT_DIR"), "/from_tuple.rs"));
+2
View File
@@ -36,4 +36,6 @@
pub mod error;
pub mod ser;
pub mod funcs;
pub mod convert;
pub use funcs::{to_file, to_out_dir, to_string, write};
+5 -5
View File
@@ -201,8 +201,8 @@ impl<W: Write> ser::Serializer for &mut Uneval<W> {
Ok(self.start_sub())
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
write!(self.writer, "(")?;
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
write!(self.writer, "::uneval::convert::convert_tuple_{}((", len)?;
Ok(self.start_sub())
}
@@ -280,7 +280,7 @@ impl<W: Write> ser::SerializeTuple for &mut Uneval<W> {
}
fn end(self) -> SerResult {
write!(self.writer, ")")?;
write!(self.writer, "))")?;
Ok(())
}
}
@@ -358,7 +358,7 @@ impl<W: Write> ser::SerializeStruct for &mut Uneval<W> {
T: serde::Serialize,
{
self.comma()?;
write!(self.writer, "{}:", key)?;
write!(self.writer, "{}: ", key)?;
value.serialize(&mut **self)?;
Ok(())
}
@@ -381,7 +381,7 @@ impl<W: Write> ser::SerializeStructVariant for &mut Uneval<W> {
T: serde::Serialize,
{
self.comma()?;
write!(self.writer, "{}:", key)?;
write!(self.writer, "{}: ", key)?;
value.serialize(&mut **self)?;
Ok(())
}
+10
View File
@@ -106,4 +106,14 @@ value = """
Enum::Struct{ key: 1, value: "test".into() },
])
}
"""
[array]
main_type = "Container"
definition = """
#[derive(PartialEq, Debug, Serialize)]
pub struct Container(pub [u8; 2]);
"""
value = """
definition::Container([1, 2])
"""