From bded2dd7405719bc24385ab2516a07d7abc07696 Mon Sep 17 00:00:00 2001 From: Evgeny Khudoba Date: Sat, 22 Feb 2025 19:29:52 +0700 Subject: [PATCH] Move `PrintBuffer` to use `RefCell` --- cursive-core/src/cursive_root.rs | 6 +++--- cursive-core/src/cursive_run.rs | 10 +++++----- cursive-core/src/printer.rs | 24 ++++++++++-------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/cursive-core/src/cursive_root.rs b/cursive-core/src/cursive_root.rs index 77e6b00a..baad0ab1 100644 --- a/cursive-core/src/cursive_root.rs +++ b/cursive-core/src/cursive_root.rs @@ -1,10 +1,10 @@ use std::any::Any; +use std::cell::RefCell; use std::num::NonZeroU32; #[cfg(feature = "toml")] use std::path::Path; use crossbeam_channel::{self, Receiver, Sender}; -use parking_lot::RwLock; use crate::{ backend, @@ -123,8 +123,8 @@ impl Cursive { self.root.layout(size); } - pub(crate) fn draw(&mut self, buffer: &RwLock) { - let size = buffer.read().size(); + pub(crate) fn draw(&mut self, buffer: &RefCell) { + let size = buffer.borrow().size(); let printer = Printer::new(size, &self.theme, buffer); diff --git a/cursive-core/src/cursive_run.rs b/cursive-core/src/cursive_run.rs index 1a3b27fb..b3493a44 100644 --- a/cursive-core/src/cursive_run.rs +++ b/cursive-core/src/cursive_run.rs @@ -1,6 +1,6 @@ use crate::{backend, buffer, event, Cursive, Vec2}; -use parking_lot::RwLock; use std::borrow::{Borrow, BorrowMut}; +use std::cell::RefCell; use std::time::Duration; // How long we wait between two empty input polls @@ -16,7 +16,7 @@ pub struct CursiveRunner { siv: C, backend: Box, - buffer: RwLock, + buffer: RefCell, boring_frame_count: u32, // Last layer sizes of the stack view. @@ -50,7 +50,7 @@ impl CursiveRunner { CursiveRunner { siv, backend, - buffer: RwLock::new(buffer::PrintBuffer::new()), + buffer: RefCell::new(buffer::PrintBuffer::new()), boring_frame_count: 0, last_sizes: Vec::new(), } @@ -93,9 +93,9 @@ where self.last_sizes = sizes; } - self.buffer.write().resize(self.screen_size()); + self.buffer.borrow_mut().resize(self.screen_size()); self.siv.borrow_mut().draw(&self.buffer); - self.buffer.write().flush(&*self.backend); + self.buffer.borrow_mut().flush(&*self.backend); } /// Performs the first half of `Self::step()`. diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index 6378fb53..976fbe03 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -14,8 +14,7 @@ use crate::with::With; use crate::Vec2; use enumset::EnumSet; -use parking_lot::RwLock; -use std::cell::Cell; +use std::cell::{Cell, RefCell}; use std::cmp::min; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; @@ -70,7 +69,7 @@ pub struct Printer<'a, 'b> { current_style: Cell, /// Backend used to actually draw things - buffer: &'b RwLock, + buffer: &'b RefCell, } impl<'a, 'b> Printer<'a, 'b> { @@ -78,7 +77,7 @@ impl<'a, 'b> Printer<'a, 'b> { /// /// But nobody needs to know that. #[doc(hidden)] - pub fn new>(size: T, theme: &'a Theme, buffer: &'b RwLock) -> Self { + pub fn new>(size: T, theme: &'a Theme, buffer: &'b RefCell) -> Self { let size = size.into(); Printer { offset: Vec2::zero(), @@ -105,7 +104,7 @@ impl<'a, 'b> Printer<'a, 'b> { /// /// This is the size of the entire terminal, not just the area this printer can write into. pub fn buffer_size(&self) -> Vec2 { - self.buffer.read().size() + self.buffer.borrow().size() } /// Clear the screen. @@ -115,7 +114,7 @@ impl<'a, 'b> Printer<'a, 'b> { /// Users rarely need to call this directly. pub fn clear(&self) { let color = self.theme.palette[PaletteColor::Background]; - self.buffer.write().fill( + self.buffer.borrow_mut().fill( " ", ColorPair { front: color, @@ -258,7 +257,7 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start + self.offset; self.buffer - .write() + .borrow_mut() .print_at(start, text, self.current_style()); } @@ -294,7 +293,7 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start + self.offset; for y in 0..height { self.buffer - .write() + .borrow_mut() .print_at(start + (0, y), c, self.current_style()); } } @@ -304,7 +303,7 @@ impl<'a, 'b> Printer<'a, 'b> { where F: FnOnce(&mut Window<'_>) -> R, { - let mut buffer = self.buffer.write(); + let mut buffer = self.buffer.borrow_mut(); let mut window = buffer .window(self.output_window()) .expect("printer size exceeds backend size"); @@ -362,7 +361,7 @@ impl<'a, 'b> Printer<'a, 'b> { let repetitions = min(width, self.output_size.x - start.x) / c_width; let mut start = start + self.offset; - let mut buffer = self.buffer.write(); + let mut buffer = self.buffer.borrow_mut(); let style = self.current_style(); for _ in 0..repetitions { buffer.print_at(start, c, style); @@ -424,10 +423,7 @@ impl<'a, 'b> Printer<'a, 'b> { /// that will apply the given color on prints. /// /// Does not change the current set of active effects (bold/underline/...). - pub fn with_color(&self, c: ColorStyle, f: F) - where - F: FnOnce(&Printer), - { + pub fn with_color(&self, c: ColorStyle, f: F) { let sub = self.clone().with(|sub| sub.set_color(c)); f(&sub);