add configs/bundle_pure

This commit is contained in:
Mark
2024-06-23 22:14:29 +02:00
parent 4d570ec5a5
commit 94111a5eaa
8 changed files with 60 additions and 33 deletions

View File

@@ -22,12 +22,6 @@ pub mod with_string;
/// bundle_* for bundles (combines multiple groups or even bundles)
/// with_* for usage-oriented groups
/// add_* to add custom things
///
/// For doc-comments:
/// Description
/// `bundle_std()`
/// `type` - description
/// `var: type` - description
pub struct Config {
globals: usize,
info_parsed: super::parsed::Info,
@@ -37,25 +31,32 @@ pub struct Config {
impl Config {
/// standard utilitis used in many programs
/// `bundle_base()`
/// `with_stdio()`
/// `with_list()`
/// `with_string()`
/// `with_command_running()`
/// `with_multithreading()`
///
/// - `bundle_pure()`
/// - `with_stdio()`
/// - `with_command_running()`
/// - `with_multithreading()`
pub fn bundle_std(self) -> Self {
self.with_multithreading()
.with_command_running()
.with_string()
.with_list()
.with_stdio()
.bundle_base()
.bundle_pure()
}
/// standard utilities, but don't allow code to do any I/O.
/// (multithreading can be added using `.with_multithreading()`)
///
/// - `bundle_base()`
/// - `with_list()`
/// - `with_string()`
pub fn bundle_pure(self) -> Self {
self.with_string().with_list().bundle_base()
}
/// base utilities used in most programs
/// `with_base()`
/// `with_math()`
/// `with_get()`
/// `with_iters()`
///
/// - `with_base()`
/// - `with_math()`
/// - `with_get()`
/// - `with_iters()`
pub fn bundle_base(self) -> Self {
self.with_iters().with_get().with_math().with_base()
}
@@ -86,6 +87,7 @@ impl Config {
}
}
/// Add a variable. Its type will be that of the value stored in `val`.
pub fn add_var(self, name: String, val: Data) -> Self {
let t = val.get().as_type();
self.add_var_arc(name, Arc::new(RwLock::new(val)), t)

View File

@@ -1,6 +1,6 @@
use std::{
sync::{Arc, Mutex, RwLock},
time::Duration,
time::{Duration, Instant},
};
use crate::{
@@ -86,15 +86,20 @@ impl Config {
} else {
Err(format!("cannot call sleep with non-int or non-float argument.").into())
}),
run: Arc::new(|a, _i| {
run: Arc::new(|a, i| {
let a = a.get();
std::thread::sleep(if let Some(data::int::Int(n)) = a.as_any().downcast_ref() {
let mut sleep_dur = if let Some(data::int::Int(n)) = a.as_any().downcast_ref() {
Duration::from_secs(*n as _)
} else if let Some(data::float::Float(n)) = a.as_any().downcast_ref() {
Duration::from_secs_f64(*n)
} else {
return Err("sleep called on non-int/non-float".into());
});
};
// limit how long sleep can take
if let Some(cutoff) = i.global.limit_runtime {
sleep_dur = sleep_dur.min(cutoff.saturating_duration_since(Instant::now()));
}
std::thread::sleep(sleep_dur);
Ok(Data::empty_tuple())
}),
inner_statements: None,

View File

@@ -464,7 +464,7 @@ fn genfunc_iter_in_val_out(
name: String,
iter_type: impl MersType + 'static,
out_type: Type,
run: impl Fn(Data, &mut crate::info::Info<program::run::Local>) -> Result<Data, CheckError>
run: impl Fn(Data, &mut crate::info::Info<program::run::RunLocal>) -> Result<Data, CheckError>
+ Send
+ Sync
+ 'static,