Reimplemented arrays unevaling with inline "runtime"
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
pub trait FromTuple<T> {
|
||||
fn from_tuple(tuple: T) -> Self;
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/from_tuple.rs"));
|
||||
@@ -0,0 +1,55 @@
|
||||
use crate::ser::SerResult;
|
||||
use std::io::Write;
|
||||
|
||||
pub(crate) fn tuple_converter(mut output: impl Write, len: usize) -> SerResult {
|
||||
write!(output, "
|
||||
trait FromTuple<T>: Sized {{
|
||||
fn from_tuple(tuple: T) -> Self;
|
||||
}}
|
||||
")?;
|
||||
let array = format!("[T; {}]", len);
|
||||
let tuple = format!("({})", (0..len).map(|_| "T,").collect::<String>());
|
||||
let mapping = format!(
|
||||
"[{}]",
|
||||
(0..len)
|
||||
.map(|index| format!("tuple.{}", index))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
);
|
||||
write!(
|
||||
output,
|
||||
"
|
||||
impl<T> FromTuple<{tuple}> for {array} {{
|
||||
#[inline]
|
||||
fn from_tuple(tuple: {tuple}) -> Self {{
|
||||
{mapping}
|
||||
}}
|
||||
}}
|
||||
",
|
||||
array = array,
|
||||
tuple = tuple,
|
||||
mapping = mapping
|
||||
)?;
|
||||
let types = (0..len)
|
||||
.map(|index| format!("T{}", index))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
write!(
|
||||
output,
|
||||
"
|
||||
impl<{types}> FromTuple<({types},)> for ({types},) {{
|
||||
#[inline]
|
||||
fn from_tuple(tuple: ({types},)) -> Self {{
|
||||
tuple
|
||||
}}
|
||||
}}
|
||||
|
||||
#[inline]
|
||||
fn convert<{types}, Out: FromTuple<({types},)>>(tuple: ({types},)) -> Out {{
|
||||
Out::from_tuple(tuple)
|
||||
}}
|
||||
",
|
||||
types = types
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
+1
-1
@@ -36,6 +36,6 @@
|
||||
pub mod error;
|
||||
pub mod ser;
|
||||
pub mod funcs;
|
||||
pub mod convert;
|
||||
pub mod helpers;
|
||||
|
||||
pub use funcs::{to_file, to_out_dir, to_string, write};
|
||||
+4
-2
@@ -202,7 +202,9 @@ impl<W: Write> ser::Serializer for &mut Uneval<W> {
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||
write!(self.writer, "::uneval::convert::convert_tuple_{}((", len)?;
|
||||
write!(self.writer, "{{")?;
|
||||
crate::helpers::tuple_converter(&mut self.writer, len)?;
|
||||
write!(self.writer, "convert((")?;
|
||||
Ok(self.start_sub())
|
||||
}
|
||||
|
||||
@@ -280,7 +282,7 @@ impl<W: Write> ser::SerializeTuple for &mut Uneval<W> {
|
||||
}
|
||||
|
||||
fn end(self) -> SerResult {
|
||||
write!(self.writer, "))")?;
|
||||
write!(self.writer, ")) }}")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user