mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-09 03:57:39 +00:00
Fix boxed cloning of AnyAction
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
ab2977c65c
commit
6129bda068
2 changed files with 16 additions and 37 deletions
|
@ -94,41 +94,16 @@ pub trait UpdateView {
|
||||||
|
|
||||||
pub trait Action: 'static + AnyAction {
|
pub trait Action: 'static + AnyAction {
|
||||||
type Argument: 'static + Clone;
|
type Argument: 'static + Clone;
|
||||||
|
|
||||||
const NAME: &'static str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AnyAction {
|
pub trait AnyAction {
|
||||||
fn id(&self) -> TypeId;
|
fn id(&self) -> TypeId;
|
||||||
|
fn name(&self) -> &'static str;
|
||||||
fn as_any(&self) -> &dyn Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
fn boxed_clone(&self) -> Box<dyn AnyAction>;
|
fn boxed_clone(&self) -> Box<dyn AnyAction>;
|
||||||
fn boxed_clone_as_any(&self) -> Box<dyn Any>;
|
fn boxed_clone_as_any(&self) -> Box<dyn Any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Action for () {
|
|
||||||
type Argument = ();
|
|
||||||
|
|
||||||
const NAME: &'static str = "()";
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AnyAction for () {
|
|
||||||
fn id(&self) -> TypeId {
|
|
||||||
TypeId::of::<()>()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boxed_clone(&self) -> Box<dyn AnyAction> {
|
|
||||||
Box::new(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boxed_clone_as_any(&self) -> Box<dyn Any> {
|
|
||||||
Box::new(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! action {
|
macro_rules! action {
|
||||||
($name:ident, $arg:ty) => {
|
($name:ident, $arg:ty) => {
|
||||||
|
@ -137,8 +112,6 @@ macro_rules! action {
|
||||||
|
|
||||||
impl $crate::Action for $name {
|
impl $crate::Action for $name {
|
||||||
type Argument = $arg;
|
type Argument = $arg;
|
||||||
|
|
||||||
const NAME: &'static str = stringify!($name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::AnyAction for $name {
|
impl $crate::AnyAction for $name {
|
||||||
|
@ -146,6 +119,10 @@ macro_rules! action {
|
||||||
std::any::TypeId::of::<$name>()
|
std::any::TypeId::of::<$name>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
stringify!($name)
|
||||||
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -166,8 +143,6 @@ macro_rules! action {
|
||||||
|
|
||||||
impl $crate::Action for $name {
|
impl $crate::Action for $name {
|
||||||
type Argument = ();
|
type Argument = ();
|
||||||
|
|
||||||
const NAME: &'static str = stringify!($name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::AnyAction for $name {
|
impl $crate::AnyAction for $name {
|
||||||
|
@ -175,16 +150,20 @@ macro_rules! action {
|
||||||
std::any::TypeId::of::<$name>()
|
std::any::TypeId::of::<$name>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
stringify!($name)
|
||||||
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
||||||
Box::new(())
|
Box::new(self.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
||||||
Box::new(())
|
Box::new(self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -455,23 +455,23 @@ mod tests {
|
||||||
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
||||||
|
|
||||||
// Multi-keystroke match
|
// Multi-keystroke match
|
||||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
|
||||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
||||||
|
|
||||||
// Failed matches don't interfere with matching subsequent keys
|
// Failed matches don't interfere with matching subsequent keys
|
||||||
assert_eq!(matcher.test_keystroke::<()>("x", 1, &ctx_a), None);
|
assert_eq!(matcher.test_keystroke::<A>("x", 1, &ctx_a), None);
|
||||||
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
||||||
|
|
||||||
// Pending keystrokes are cleared when the context changes
|
// Pending keystrokes are cleared when the context changes
|
||||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
|
||||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_a), Some(B));
|
assert_eq!(matcher.test_keystroke("b", 1, &ctx_a), Some(B));
|
||||||
|
|
||||||
let mut ctx_c = Context::default();
|
let mut ctx_c = Context::default();
|
||||||
ctx_c.set.insert("c".into());
|
ctx_c.set.insert("c".into());
|
||||||
|
|
||||||
// Pending keystrokes are maintained per-view
|
// Pending keystrokes are maintained per-view
|
||||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
|
||||||
assert_eq!(matcher.test_keystroke::<()>("a", 2, &ctx_c), None);
|
assert_eq!(matcher.test_keystroke::<A>("a", 2, &ctx_c), None);
|
||||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue