1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +03:00

wezterm: refactor: move shapecache into own module

This commit is contained in:
Wez Furlong 2020-10-19 09:34:58 -07:00
parent eaa6536325
commit 7314daa875
3 changed files with 76 additions and 73 deletions

View File

@ -11,6 +11,7 @@ mod quad;
mod renderstate; mod renderstate;
mod scrollbar; mod scrollbar;
mod selection; mod selection;
mod shapecache;
mod tabbar; mod tabbar;
mod termwindow; mod termwindow;
mod utilsprites; mod utilsprites;

View File

@ -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:
/// <https://github.com/sunshowers/borrow-complex-key-example/blob/master/src/lib.rs>
#[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<dyn ShapeCacheKeyTrait + 'a> for ShapeCacheKey {
fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) {
self
}
}
impl<'a> std::borrow::Borrow<dyn ShapeCacheKeyTrait + 'a> for lru::KeyRef<ShapeCacheKey> {
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<H: std::hash::Hasher>(&self, state: &mut H) {
self.key().hash(state)
}
}

View File

@ -11,6 +11,7 @@ use crate::gui::overlay::{
}; };
use crate::gui::scrollbar::*; use crate::gui::scrollbar::*;
use crate::gui::selection::*; use crate::gui::selection::*;
use crate::gui::shapecache::*;
use crate::gui::tabbar::{TabBarItem, TabBarState}; use crate::gui::tabbar::{TabBarItem, TabBarState};
use crate::scripting::guiwin::GuiWin; use crate::scripting::guiwin::GuiWin;
use crate::scripting::pane::PaneObject; use crate::scripting::pane::PaneObject;
@ -29,7 +30,7 @@ use anyhow::{anyhow, bail, ensure};
use config::keyassignment::{ use config::keyassignment::{
InputMap, KeyAssignment, MouseEventTrigger, SpawnCommand, SpawnTabDomain, InputMap, KeyAssignment, MouseEventTrigger, SpawnCommand, SpawnTabDomain,
}; };
use config::{configuration, ConfigHandle, TextStyle}; use config::{configuration, ConfigHandle};
use lru::LruCache; use lru::LruCache;
use mux::activity::Activity; use mux::activity::Activity;
use mux::domain::{DomainId, DomainState}; use mux::domain::{DomainId, DomainState};
@ -190,78 +191,6 @@ pub struct TabState {
pub overlay: Option<Rc<dyn Pane>>, pub overlay: Option<Rc<dyn Pane>>,
} }
#[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:
/// <https://github.com/sunshowers/borrow-complex-key-example/blob/master/src/lib.rs>
#[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<dyn ShapeCacheKeyTrait + 'a> for ShapeCacheKey {
fn borrow(&self) -> &(dyn ShapeCacheKeyTrait + 'a) {
self
}
}
impl<'a> std::borrow::Borrow<dyn ShapeCacheKeyTrait + 'a> for lru::KeyRef<ShapeCacheKey> {
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<H: std::hash::Hasher>(&self, state: &mut H) {
self.key().hash(state)
}
}
pub struct TermWindow { pub struct TermWindow {
pub window: Option<Window>, pub window: Option<Window>,
/// When we most recently received keyboard focus /// When we most recently received keyboard focus