Implemented engine return

This commit is contained in:
2026-01-30 14:19:24 +01:00
parent ccbbf29538
commit 2d79b1dfbb
7 changed files with 37 additions and 30 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ use osui::prelude::*;
pub fn main() { pub fn main() {
let engine = Benchmark::new(Console::new()); let engine = Benchmark::new(Console::new());
engine.run(app); engine.run(app).expect("Failed to run engine");
} }
fn app(cx: &Arc<Context>) -> View { fn app(cx: &Arc<Context>) -> View {
+2 -2
View File
@@ -3,8 +3,8 @@ use std::sync::Arc;
use osui::prelude::*; use osui::prelude::*;
pub fn main() { pub fn main() {
let console = Console::new(); let engine = Console::new();
console.run(app); engine.run(app).expect("Failed to run engine");
} }
fn app(cx: &Arc<Context>) -> View { fn app(cx: &Arc<Context>) -> View {
+1 -1
View File
@@ -4,7 +4,7 @@ use osui::prelude::*;
pub fn main() { pub fn main() {
let engine = SimpleBenchmark::new(Console::new()); let engine = SimpleBenchmark::new(Console::new());
engine.run(app); engine.run(app).expect("Failed to run engine");
} }
fn app(cx: &Arc<Context>) -> View { fn app(cx: &Arc<Context>) -> View {
+16 -15
View File
@@ -15,7 +15,10 @@ impl<T: Engine> Benchmark<T> {
} }
impl<T: Engine> Engine for Benchmark<T> { impl<T: Engine> Engine for Benchmark<T> {
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) { fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
&self,
component: F,
) -> crate::Result<()> {
let mut times: Vec<u64> = Vec::new(); let mut times: Vec<u64> = Vec::new();
let cx = self.init(component); let cx = self.init(component);
@@ -25,7 +28,7 @@ impl<T: Engine> Engine for Benchmark<T> {
let start = Instant::now(); let start = Instant::now();
self.render(&cx); self.render(&cx);
let end = Instant::now(); let end = Instant::now();
times.push(end.duration_since(start).as_nanos() as u64); times.push(end.duration_since(start).as_micros() as u64);
self.0.render_delay(); self.0.render_delay();
} }
@@ -36,27 +39,25 @@ impl<T: Engine> Engine for Benchmark<T> {
execute!(stdout(), MoveTo(0, 0)).unwrap(); execute!(stdout(), MoveTo(0, 0)).unwrap();
println!( println!(
"Average time: {} nanoseconds", "Average time: {} µs",
if times.len() > 0 { if times.len() > 0 {
times.iter().sum::<u64>() / (times.len() as u64) times.iter().sum::<u64>() / (times.len() as u64)
} else { } else {
0 0
} }
); );
println!("Min time: {} nanoseconds", times.iter().min().unwrap_or(&0)); println!("Min time: {} µs", times.iter().min().unwrap_or(&0));
println!("Max time: {} nanoseconds", times.iter().max().unwrap_or(&0)); println!("Max time: {} µs", times.iter().max().unwrap_or(&0));
println!("Total time: {} µs", end.duration_since(start).as_micros());
println!("Total render time: {} µs", times.iter().sum::<u64>());
println!( println!(
"Median time: {} nanoseconds", "\n{},{},{}",
times.iter().nth(times.len() / 2).unwrap_or(&0) times.iter().min().unwrap_or(&0),
); times.iter().max().unwrap_or(&0),
println!( end.duration_since(start).as_micros()
"Total time: {} nanoseconds",
end.duration_since(start).as_nanos()
);
println!(
"Total render time: {} nanoseconds",
times.iter().sum::<u64>()
); );
Ok(())
} }
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>( fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
+6 -1
View File
@@ -102,13 +102,18 @@ impl Engine for Console {
self.executor.clone() self.executor.clone()
} }
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) { fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
&self,
component: F,
) -> crate::Result<()> {
let cx = self.init(component); let cx = self.init(component);
while self.executor.is_running() { while self.executor.is_running() {
self.render(&cx); self.render(&cx);
self.render_delay(); self.render_delay();
} }
Ok(())
} }
} }
+5 -9
View File
@@ -11,15 +11,11 @@ use std::{any::Any, sync::Arc};
use crate::{prelude::Context, render::Area, DrawContext, View}; use crate::{prelude::Context, render::Area, DrawContext, View};
pub trait Engine { pub trait Engine<Output = ()> {
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) { fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
let cx = self.init(component); &self,
component: F,
loop { ) -> crate::Result<Output>;
self.render(&cx);
self.render_delay();
}
}
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>( fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
&self, &self,
+6 -1
View File
@@ -15,7 +15,10 @@ impl<T: Engine> SimpleBenchmark<T> {
} }
impl<T: Engine> Engine for SimpleBenchmark<T> { impl<T: Engine> Engine for SimpleBenchmark<T> {
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) { fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
&self,
component: F,
) -> crate::Result<()> {
let mut times: Vec<u64> = Vec::new(); let mut times: Vec<u64> = Vec::new();
let cx = self.init(component); let cx = self.init(component);
@@ -57,6 +60,8 @@ impl<T: Engine> Engine for SimpleBenchmark<T> {
"Total render time: {} nanoseconds", "Total render time: {} nanoseconds",
times.iter().sum::<u64>() times.iter().sum::<u64>()
); );
Ok(())
} }
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>( fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(