From 7314daa875511cd0522b2c4700fa9e63ab000659 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 19 Oct 2020 09:34:58 -0700 Subject: [PATCH] wezterm: refactor: move shapecache into own module --- wezterm/src/gui/mod.rs | 1 + wezterm/src/gui/shapecache.rs | 73 ++++++++++++++++++++++++++++++++++ wezterm/src/gui/termwindow.rs | 75 +---------------------------------- 3 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 wezterm/src/gui/shapecache.rs diff --git a/wezterm/src/gui/mod.rs b/wezterm/src/gui/mod.rs index 5a616ab98..b6badee2d 100644 --- a/wezterm/src/gui/mod.rs +++ b/wezterm/src/gui/mod.rs @@ -11,6 +11,7 @@ mod quad; mod renderstate; mod scrollbar; mod selection; +mod shapecache; mod tabbar; mod termwindow; mod utilsprites; diff --git a/wezterm/src/gui/shapecache.rs b/wezterm/src/gui/shapecache.rs new file mode 100644 index 000000000..0a5f54d25 --- /dev/null +++ b/wezterm/src/gui/shapecache.rs @@ -0,0 +1,73 @@ +use config::TextStyle; + +#[derive(PartialEq, Eq, Hash)] +pub struct ShapeCacheKey { + pub style: TextStyle, + pub text: String, +} + +/// We'd like to avoid allocating when resolving from the cache +/// so this is the borrowed version of ShapeCacheKey. +/// It's a bit involved to make this work; more details can be +/// found in the excellent guide here: +/// +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub struct BorrowedShapeCacheKey<'a> { + pub style: &'a TextStyle, + pub text: &'a str, +} + +impl<'a> BorrowedShapeCacheKey<'a> { + pub fn to_owned(&self) -> ShapeCacheKey { + ShapeCacheKey { + style: self.style.clone(), + text: self.text.to_owned(), + } + } +} + +pub trait ShapeCacheKeyTrait { + fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k>; +} + +impl ShapeCacheKeyTrait for ShapeCacheKey { + fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k> { + BorrowedShapeCacheKey { + style: &self.style, + text: &self.text, + } + } +} + +impl<'a> ShapeCacheKeyTrait for BorrowedShapeCacheKey<'a> { + fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k> { + *self + } +} + +impl<'a> std::borrow::Borrow for ShapeCacheKey { + fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) { + self + } +} + +impl<'a> std::borrow::Borrow for lru::KeyRef { + fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) { + let k: &ShapeCacheKey = self.borrow(); + k + } +} + +impl<'a> PartialEq for (dyn ShapeCacheKeyTrait + 'a) { + fn eq(&self, other: &Self) -> bool { + self.key().eq(&other.key()) + } +} + +impl<'a> Eq for (dyn ShapeCacheKeyTrait + 'a) {} + +impl<'a> std::hash::Hash for (dyn ShapeCacheKeyTrait + 'a) { + fn hash(&self, state: &mut H) { + self.key().hash(state) + } +} diff --git a/wezterm/src/gui/termwindow.rs b/wezterm/src/gui/termwindow.rs index 2aa83c576..a33af037f 100644 --- a/wezterm/src/gui/termwindow.rs +++ b/wezterm/src/gui/termwindow.rs @@ -11,6 +11,7 @@ use crate::gui::overlay::{ }; use crate::gui::scrollbar::*; use crate::gui::selection::*; +use crate::gui::shapecache::*; use crate::gui::tabbar::{TabBarItem, TabBarState}; use crate::scripting::guiwin::GuiWin; use crate::scripting::pane::PaneObject; @@ -29,7 +30,7 @@ use anyhow::{anyhow, bail, ensure}; use config::keyassignment::{ InputMap, KeyAssignment, MouseEventTrigger, SpawnCommand, SpawnTabDomain, }; -use config::{configuration, ConfigHandle, TextStyle}; +use config::{configuration, ConfigHandle}; use lru::LruCache; use mux::activity::Activity; use mux::domain::{DomainId, DomainState}; @@ -190,78 +191,6 @@ pub struct TabState { pub overlay: Option>, } -#[derive(PartialEq, Eq, Hash)] -struct ShapeCacheKey { - style: TextStyle, - text: String, -} - -/// We'd like to avoid allocating when resolving from the cache -/// so this is the borrowed version of ShapeCacheKey. -/// It's a bit involved to make this work; more details can be -/// found in the excellent guide here: -/// -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -struct BorrowedShapeCacheKey<'a> { - style: &'a TextStyle, - text: &'a str, -} - -impl<'a> BorrowedShapeCacheKey<'a> { - fn to_owned(&self) -> ShapeCacheKey { - ShapeCacheKey { - style: self.style.clone(), - text: self.text.to_owned(), - } - } -} - -trait ShapeCacheKeyTrait { - fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k>; -} - -impl ShapeCacheKeyTrait for ShapeCacheKey { - fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k> { - BorrowedShapeCacheKey { - style: &self.style, - text: &self.text, - } - } -} - -impl<'a> ShapeCacheKeyTrait for BorrowedShapeCacheKey<'a> { - fn key<'k>(&'k self) -> BorrowedShapeCacheKey<'k> { - *self - } -} - -impl<'a> std::borrow::Borrow for ShapeCacheKey { - fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) { - self - } -} - -impl<'a> std::borrow::Borrow for lru::KeyRef { - fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) { - let k: &ShapeCacheKey = self.borrow(); - k - } -} - -impl<'a> PartialEq for (dyn ShapeCacheKeyTrait + 'a) { - fn eq(&self, other: &Self) -> bool { - self.key().eq(&other.key()) - } -} - -impl<'a> Eq for (dyn ShapeCacheKeyTrait + 'a) {} - -impl<'a> std::hash::Hash for (dyn ShapeCacheKeyTrait + 'a) { - fn hash(&self, state: &mut H) { - self.key().hash(state) - } -} - pub struct TermWindow { pub window: Option, /// When we most recently received keyboard focus