Skip to content

Commit

Permalink
Move runtime to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Spartan2909 committed Jul 15, 2024
1 parent 9af3899 commit b08921e
Showing 1 changed file with 155 additions and 151 deletions.
306 changes: 155 additions & 151 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,198 +609,202 @@ impl ToTokens for Process {
}
}

impl ToTokens for Program {
fn to_tokens(&self, tokens: &mut TokenStream) {
let csv = &self.csv;
fn runtime() -> TokenStream {
quote! {
extern crate alloc;
use ::core::prelude::rust_2021::*;
use alloc::{boxed::Box, collections::VecDeque, vec, vec::Vec};

enum Interrupt {
Delete,
Error(String),
}

#[cfg(feature = "benchmark")]
let mut inner = quote! {
#[cfg(debug_assertions)]
use ::std::{time::Instant, println};
};
impl Interrupt {
fn extract_error(self) -> String {
if let Interrupt::Error(message) = self {
message
} else {
panic!("attempted to extract error from 'Delete'")
}
}
}

#[cfg(not(feature = "benchmark"))]
let mut inner = TokenStream::new();
#[derive(Clone, Copy, PartialEq)]
enum Action {
AppendValid,
IncrementInvalid,
}

let process_function_input_type = if self.string_input {
quote!(&[Vec<&str>])
} else {
let mut file_type = TokenStream::new();
trait SanitiseConversions {
fn to_bool(&self) -> Result<bool, Interrupt>;
fn to_float(&self) -> Result<f64, Interrupt>;
fn to_int(&self) -> Result<i64, Interrupt>;
fn to_string(&self) -> Result<String, Interrupt>;
}

for column_type in self.processes[0].column_types() {
file_type.extend(quote!(&[Option<#column_type>],));
impl SanitiseConversions for bool {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self)
}

quote!((#file_type))
};
#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
if *self {
Ok(1.0)
} else {
Ok(0.0)
}
}

let main_function_input_type = if self.string_input {
quote!(&str)
} else {
#[allow(clippy::redundant_clone)]
process_function_input_type.clone()
};
#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
if *self {
Ok(1)
} else {
Ok(0)
}
}

inner.extend(quote! {
extern crate alloc;
use ::core::prelude::rust_2021::*;
use alloc::{boxed::Box, collections::VecDeque, vec, vec::Vec};
#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
}

enum Interrupt {
Delete,
Error(String),
impl SanitiseConversions for f64 {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self != 0.0)
}

impl Interrupt {
fn extract_error(self) -> String {
if let Interrupt::Error(message) = self {
message
} else {
panic!("attempted to extract error from 'Delete'")
}
}
#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
Ok(*self)
}

#[derive(Clone, Copy, PartialEq)]
enum Action {
AppendValid,
IncrementInvalid,
#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
Ok(*self as i64)
}

trait SanitiseConversions {
fn to_bool(&self) -> Result<bool, Interrupt>;
fn to_float(&self) -> Result<f64, Interrupt>;
fn to_int(&self) -> Result<i64, Interrupt>;
fn to_string(&self) -> Result<String, Interrupt>;
#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
}

impl SanitiseConversions for bool {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self)
}
impl SanitiseConversions for i64 {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self != 0)
}

#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
if *self {
Ok(1.0)
} else {
Ok(0.0)
}
}
#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
Ok(*self as f64)
}

#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
if *self {
Ok(1)
} else {
Ok(0)
}
}
#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
Ok(*self)
}

#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
}

impl SanitiseConversions for f64 {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self != 0.0)
}
impl SanitiseConversions for String {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(!self.is_empty())
}

#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
Ok(*self)
fn to_float(&self) -> Result<f64, Interrupt> {
if let Ok(n) = self.parse() {
Ok(n)
} else {
let message = format!("invalid base for float: '{self}'");
Err(Interrupt::Error(message))
}
}

#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
Ok(*self as i64)
fn to_int(&self) -> Result<i64, Interrupt> {
if let Ok(n) = self.parse() {
Ok(n)
} else {
let message = format!("invalid base for int: '{self}'");
Err(Interrupt::Error(message))
}
}

#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(self.to_owned())
}
}

impl SanitiseConversions for i64 {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(*self != 0)
}
#[inline(always)]
fn sanitise_ceiling(value: &f64) -> f64 {
(*value).ceil()
}

#[inline(always)]
fn to_float(&self) -> Result<f64, Interrupt> {
Ok(*self as f64)
}
#[inline(always)]
fn sanitise_floor(value: &f64) -> f64 {
(*value).floor()
}

#[inline(always)]
fn to_int(&self) -> Result<i64, Interrupt> {
Ok(*self)
}
#[inline(always)]
fn sanitise_round(value: &f64) -> f64 {
(*value).round()
}

#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(ToString::to_string(self))
}
}
#[inline(always)]
fn sanitise_concat(value1: &str, value2: &str) -> String {
let mut output = String::with_capacity(value1.len() + value2.len());
output.push_str(value1);
output.push_str(value2);
output
}
}
}

impl SanitiseConversions for String {
#[inline(always)]
fn to_bool(&self) -> Result<bool, Interrupt> {
Ok(!self.is_empty())
}
impl ToTokens for Program {
fn to_tokens(&self, tokens: &mut TokenStream) {
let csv = &self.csv;

fn to_float(&self) -> Result<f64, Interrupt> {
if let Ok(n) = self.parse() {
Ok(n)
} else {
let message = format!("invalid base for float: '{self}'");
Err(Interrupt::Error(message))
}
}
#[cfg(feature = "benchmark")]
let mut inner = quote! {
#[cfg(debug_assertions)]
use ::std::{time::Instant, println};
};

fn to_int(&self) -> Result<i64, Interrupt> {
if let Ok(n) = self.parse() {
Ok(n)
} else {
let message = format!("invalid base for int: '{self}'");
Err(Interrupt::Error(message))
}
}
#[cfg(not(feature = "benchmark"))]
let mut inner = TokenStream::new();

#[inline(always)]
fn to_string(&self) -> Result<String, Interrupt> {
Ok(self.to_owned())
}
}
let process_function_input_type = if self.string_input {
quote!(&[Vec<&str>])
} else {
let mut file_type = TokenStream::new();

#[inline(always)]
fn sanitise_ceiling(value: &f64) -> f64 {
(*value).ceil()
for column_type in self.processes[0].column_types() {
file_type.extend(quote!(&[Option<#column_type>],));
}

#[inline(always)]
fn sanitise_floor(value: &f64) -> f64 {
(*value).floor()
}
quote!((#file_type))
};

#[inline(always)]
fn sanitise_round(value: &f64) -> f64 {
(*value).round()
}
let main_function_input_type = if self.string_input {
quote!(&str)
} else {
#[allow(clippy::redundant_clone)]
process_function_input_type.clone()
};

#[inline(always)]
fn sanitise_concat(value1: &str, value2: &str) -> String {
let mut output = String::with_capacity(value1.len() + value2.len());
output.push_str(value1);
output.push_str(value2);
output
}
});
inner.extend(runtime());

let mut process_data = vec![];
let mut signature = TokenStream::new();
Expand Down

0 comments on commit b08921e

Please sign in to comment.