relax Send + Sync constraint on Renderables; remove 3 format calls from render loop

maybe render should have mutable access after all?
This commit is contained in:
🪞👃🪞 2025-01-20 18:53:15 +01:00
parent 209f35440a
commit 680a841e3f
5 changed files with 58 additions and 55 deletions

View file

@ -21,13 +21,13 @@ pub trait Output: Send + Sync + Sized {
#[inline] fn wh (&self) -> Self::Size { self.area().wh().into() }
}
/// Renderable with dynamic dispatch.
pub trait Render<E: Output>: Send + Sync {
pub trait Render<E: Output> {
/// Compute layout.
fn layout (&self, area: E::Area) -> E::Area;
/// Write data to display.
fn render (&self, output: &mut E);
/// Perform type erasure, turning `self` into an opaque [RenderBox].
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Sized + 'a {
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Send + Sync + Sized + 'a {
Box::new(self) as RenderBox<'a, E>
}
}
@ -50,7 +50,7 @@ impl<'a, E: Output> Content<E> for RenderBox<'a, E> {
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
}
/// Opaque pointer to a renderable.
pub type RenderDyn<'a, E> = dyn Render<E> + 'a;
pub type RenderDyn<'a, E> = dyn Render<E> + Send + Sync + 'a;
/// You can render from an opaque pointer.
impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> { self.deref() }
@ -58,7 +58,7 @@ impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn render (&self, output: &mut E) { Render::render(self.deref(), output) }
}
/// Composable renderable with static dispatch.
pub trait Content<E: Output>: Send + Sync + Sized {
pub trait Content<E: Output> {
/// Return a [Render]able of a specific type.
fn content (&self) -> impl Render<E> { () }
/// Perform layout. By default, delegates to [Self::content].

View file

@ -12,9 +12,9 @@ impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E,
fn content (&self) -> impl Render<E> { (self.1)() }
}
pub struct ThunkBox<'a, E: Output>(PhantomData<E>, Box<dyn Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a>);
pub struct ThunkBox<'a, E: Output>(PhantomData<E>, Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>);
impl<'a, E: Output> ThunkBox<'a, E> {
pub fn new (thunk: Box<dyn Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a>) -> Self {
pub fn new (thunk: Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>) -> Self {
Self(Default::default(), thunk)
}
}

View file

@ -151,9 +151,9 @@ impl TekCli {
pub keys_scene: SourceIter<'static>,
pub keys_mix: SourceIter<'static>,
pub fmt_beat: String,
pub fmt_time: String,
pub fmt_bpm: String,
pub fmt_beat: RwLock<String>,
pub fmt_time: RwLock<String>,
pub fmt_bpm: RwLock<String>,
}
has_size!(<TuiOut>|self: Tek|&self.size);
has_clock!(|self: Tek|self.clock);
@ -229,9 +229,9 @@ impl Tek {
keys_track: SourceIter(KEYS_TRACK),
keys_scene: SourceIter(KEYS_SCENE),
keys_mix: SourceIter(KEYS_MIX),
fmt_beat: String::with_capacity(16),
fmt_time: String::with_capacity(16),
fmt_bpm: String::with_capacity(16),
fmt_beat: String::with_capacity(16).into(),
fmt_time: String::with_capacity(16).into(),
fmt_bpm: String::with_capacity(16).into(),
..Default::default()
};
tek.sync_lead(sync_lead);
@ -381,24 +381,31 @@ impl Tek {
}
fn view_beat_stats (&self) -> impl Content<TuiOut> + use<'_> {
let compact = self.size.w() > 80;
let clock = self.clock();
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
let clock = self.clock();
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
let mut fmt_beat = self.fmt_beat.write().unwrap();
let mut fmt_time = self.fmt_time.write().unwrap();
let mut fmt_bpm = self.fmt_bpm.write().unwrap();
fmt_beat.clear();
fmt_time.clear();
fmt_bpm.clear();
if let Some(now) = clock.started.read().unwrap().as_ref().map(delta) {
clock.timebase.format_beats_1_to(&mut self.fmt_beat, clock.timebase.usecs_to_pulse(now));
write!(&mut self.fmt_time, "{:.3}s", now/1000000.);
write!(&mut self.fmt_bpm, "{:.3}", clock.timebase.bpm.get());
clock.timebase.format_beats_1_to(&mut*fmt_beat, clock.timebase.usecs_to_pulse(now));
write!(&mut fmt_time, "{:.3}s", now/1000000.);
write!(&mut fmt_bpm, "{:.3}", clock.timebase.bpm.get());
} else {
write!(&mut self.fmt_beat, "-.-.--");
write!(&mut self.fmt_time, "-.---s");
write!(&mut self.fmt_bpm, "---.---");
write!(&mut fmt_beat, "-.-.--");
write!(&mut fmt_time, "-.---s");
write!(&mut fmt_bpm, "---.---");
}
let theme = ItemPalette::G[128];
let fh = |name, value|Field(theme, name, value);
let fv = |name, value|FieldV(theme, name, value);
Either::new(compact,
row!(fh("BPM", &self.fmt_bpm), fh("Beat", &self.fmt_beat), fh("Time", &self.fmt_time)),
row!(fv("BPM", &self.fmt_bpm), fv("Beat", &self.fmt_beat), fv("Time", &self.fmt_time))
)
Thunk::new(move||Either::new(compact,
row!(FieldH(theme, "BPM", self.fmt_bpm.read().unwrap()),
FieldH(theme, "Beat", self.fmt_beat.read().unwrap()),
FieldH(theme, "Time", self.fmt_time.read().unwrap())),
row!(FieldV(theme, "BPM", self.fmt_bpm.read().unwrap()),
FieldV(theme, "Beat", self.fmt_beat.read().unwrap()),
FieldV(theme, "Time", self.fmt_time.read().unwrap()))))
}
fn view_engine_stats (&self) -> impl Content<TuiOut> + use<'_> {
let compact = self.size.w() > 80;
@ -410,12 +417,12 @@ impl Tek {
let latency = move||format!("{:.1}ms", chunk as f64 / rate * 1000.);
let theme = ItemPalette::G[128];
Either::new(compact,
row!(Field(theme, "SR", sr()), Field(theme, "Buf", buf()), Field(theme, "Lat", latency())),
row!(FieldH(theme, "SR", sr()), FieldH(theme, "Buf", buf()), FieldH(theme, "Lat", latency())),
row!(FieldV(theme, "SR", sr()), FieldV(theme, "Buf", buf()), FieldV(theme, "Lat", latency())))
}
fn view_meter <'a> (&'a self, label: &'a str, value: f32) -> impl Content<TuiOut> + 'a {
col!(
Field(ItemPalette::G[128], label, format!("{:>+9.3}", value)),
FieldH(ItemPalette::G[128], label, format!("{:>+9.3}", value)),
Fixed::xy(if value >= 0.0 { 13 }
else if value >= -1.0 { 12 }
else if value >= -2.0 { 11 }

View file

@ -20,6 +20,15 @@ impl Content<TuiOut> for String {
}
}
impl Content<TuiOut> for std::sync::RwLockReadGuard<'_, String> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
Content::<TuiOut>::layout(&**self, to)
}
fn render (&self, to: &mut TuiOut) {
Content::<TuiOut>::render(&**self, to)
}
}
impl Content<TuiOut> for Arc<str> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_xy([self.chars().count() as u16, 1])
@ -29,43 +38,32 @@ impl Content<TuiOut> for Arc<str> {
}
}
pub struct Field<T, U>(pub ItemPalette, pub T, pub U)
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync;
impl<T, U> Content<TuiOut> for Field<T, U>
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync
{
pub struct FieldH<T, U>(pub ItemPalette, pub T, pub U);
impl<T: Content<TuiOut>, U: Content<TuiOut>> Content<TuiOut> for FieldH<T, U> {
fn content (&self) -> impl Render<TuiOut> {
let ItemPalette { darkest, dark, lighter, lightest, .. } = self.0;
let Self(ItemPalette { darkest, dark, lighter, lightest, .. }, title, value) = self;
row!(
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lighter.rgb, dark.rgb, Tui::bold(true, format!("{}", self.1.as_ref()))),
Tui::fg_bg(lighter.rgb, dark.rgb, Tui::bold(true, title)),
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lightest.rgb, darkest.rgb, format!("{} ", self.2.as_ref()))
Tui::fg_bg(lightest.rgb, darkest.rgb, value),
)
}
}
pub struct FieldV<T, U>(pub ItemPalette, pub T, pub U)
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync;
impl<T, U> Content<TuiOut> for FieldV<T, U>
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync
{
pub struct FieldV<T, U>(pub ItemPalette, pub T, pub U);
impl<T: Content<TuiOut>, U: Content<TuiOut>> Content<TuiOut> for FieldV<T, U> {
fn content (&self) -> impl Render<TuiOut> {
let ItemPalette { darkest, dark, lighter, lightest, .. } = self.0;
let sep1 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let sep2 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let name = Tui::bg(dark.rgb, Tui::fg(lighter.rgb,
Tui::bold(true, format!("{}", self.1.as_ref()))));
let value = Tui::bg(darkest.rgb, Tui::fg(lightest.rgb,
format!(" {} ", self.2.as_ref())));
Bsp::e(Bsp::s(row!(sep1, name, sep2), value), " ")
let Self(ItemPalette { darkest, dark, lighter, lightest, .. }, title, value) = self;
let sep1 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let sep2 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let title = Tui::bg(dark.rgb, Tui::fg(lighter.rgb, Tui::bold(true, title)));
let value = Tui::bg(darkest.rgb, Tui::fg(lightest.rgb, value));
Bsp::e(Bsp::s(row!(sep1, title, sep2), value), " ")
}
}
pub struct Repeat<'a>(pub &'a str);
impl Content<TuiOut> for Repeat<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
@ -85,7 +83,6 @@ impl Content<TuiOut> for Repeat<'_> {
}
pub struct RepeatV<'a>(pub &'a str);
impl Content<TuiOut> for RepeatV<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
@ -101,7 +98,6 @@ impl Content<TuiOut> for RepeatV<'_> {
}
pub struct RepeatH<'a>(pub &'a str);
impl Content<TuiOut> for RepeatH<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to

View file

@ -21,7 +21,7 @@ impl Output for TuiOut {
}
impl TuiOut {
/// Spawn the output thread.
pub fn run_output <T: Render<TuiOut> + 'static> (
pub fn run_output <T: Render<TuiOut> + Send + Sync + 'static> (
engine: &Arc<RwLock<Tui>>,
state: &Arc<RwLock<T>>,
timer: Duration