diff --git a/build.rs b/build.rs deleted file mode 100644 index 1de6189..0000000 --- a/build.rs +++ /dev/null @@ -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::()); - let mapping = format!("[{}]", (0..i).map(|index| format!("tuple.{}", index)).collect::>().join(",")); - write!(convert, " - impl 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::>().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"); - } -} \ No newline at end of file diff --git a/src/convert.rs b/src/convert.rs deleted file mode 100644 index 528ecce..0000000 --- a/src/convert.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub trait FromTuple { - fn from_tuple(tuple: T) -> Self; -} - -include!(concat!(env!("OUT_DIR"), "/from_tuple.rs")); \ No newline at end of file diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..8b5ddaa --- /dev/null +++ b/src/helpers.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: Sized {{ + fn from_tuple(tuple: T) -> Self; + }} + ")?; + let array = format!("[T; {}]", len); + let tuple = format!("({})", (0..len).map(|_| "T,").collect::()); + let mapping = format!( + "[{}]", + (0..len) + .map(|index| format!("tuple.{}", index)) + .collect::>() + .join(",") + ); + write!( + output, + " + impl 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::>() + .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(()) +} diff --git a/src/lib.rs b/src/lib.rs index 1d1dbb5..6988fc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; \ No newline at end of file diff --git a/src/ser.rs b/src/ser.rs index bd0f6a3..60657ad 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -202,7 +202,9 @@ impl ser::Serializer for &mut Uneval { } fn serialize_tuple(self, len: usize) -> Result { - 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 ser::SerializeTuple for &mut Uneval { } fn end(self) -> SerResult { - write!(self.writer, "))")?; + write!(self.writer, ")) }}")?; Ok(()) } }