Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore removing SmallVec from Event enum #614

Merged
merged 4 commits into from
Apr 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -36,8 +36,6 @@ unicode-width = "0.1"
unicode-segmentation = "1.0"
memchr = "2.0"
# For custom bindings
# https://rustsec.org/advisories/RUSTSEC-2021-0003.html
smallvec = "1.6.1"
radix_trie = "0.2"
regex = { version = "1.5.4", optional = true }

3 changes: 1 addition & 2 deletions examples/custom_key_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use smallvec::smallvec;
use std::borrow::Cow::{self, Borrowed, Owned};

use rustyline::highlight::Highlighter;
@@ -105,7 +104,7 @@ fn main() -> Result<()> {
EventHandler::Conditional(Box::new(TabEventHandler)),
);
rl.bind_sequence(
Event::KeySeq(smallvec![KeyEvent::ctrl('X'), KeyEvent::ctrl('E')]),
Event::KeySeq(vec![KeyEvent::ctrl('X'), KeyEvent::ctrl('E')]),
EventHandler::Simple(Cmd::Suspend), // TODO external editor
);

16 changes: 10 additions & 6 deletions src/binding.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ use crate::{
};

use radix_trie::TrieKey;
use smallvec::{smallvec, SmallVec};

/// Input event
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -13,8 +12,7 @@ pub enum Event {
/// Useful if you want to filter out some keys.
Any,
/// Key sequence
// TODO Validate 2 ?
KeySeq(SmallVec<[KeyEvent; 2]>),
KeySeq(Vec<KeyEvent>),
/// TODO Mouse event
Mouse(),
}
@@ -43,7 +41,7 @@ impl Event {

impl From<KeyEvent> for Event {
fn from(k: KeyEvent) -> Event {
Event::KeySeq(smallvec![k])
Event::KeySeq(vec![k])
}
}

@@ -214,15 +212,16 @@ pub trait ConditionalEventHandler: Send + Sync {

#[cfg(test)]
mod test {
use core::mem::size_of;

use super::{Event, EventHandler};
use crate::{Cmd, KeyCode, KeyEvent, Modifiers};
use radix_trie::Trie;
use smallvec::smallvec;

#[test]
fn encode() {
let mut trie = Trie::new();
let evt = Event::KeySeq(smallvec![KeyEvent::ctrl('X'), KeyEvent::ctrl('E')]);
let evt = Event::KeySeq(vec![KeyEvent::ctrl('X'), KeyEvent::ctrl('E')]);
trie.insert(evt.clone(), EventHandler::from(Cmd::Noop));
let prefix = Event::from(KeyEvent::ctrl('X'));
let subtrie = trie.get_raw_descendant(&prefix);
@@ -247,4 +246,9 @@ mod test {
trie.insert(E::from(K(C::Enter, M::CTRL)), H::from(Cmd::Noop));
trie.insert(E::from(K(C::Tab, M::CTRL)), H::from(Cmd::Noop));
}

#[test]
fn size_of_event() {
assert_eq!(size_of::<Event>(), 32);
}
}