diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rs index 2a70674daceea64f17cc26ae804a88365791f703..cffff292c77edbd9fe6d4cfd38c5d43d4ebf0062 100644 --- a/crates/copilot/src/copilot.rs +++ b/crates/copilot/src/copilot.rs @@ -1280,10 +1280,6 @@ mod tests { unimplemented!() } - fn as_any(&self) -> &dyn std::any::Any { - unimplemented!() - } - fn to_proto(&self, _: &App) -> rpc::proto::File { unimplemented!() } diff --git a/crates/git_ui/src/commit_view.rs b/crates/git_ui/src/commit_view.rs index 1dde3ca2b782b8eca99e265731158684dacbf045..06e4c92460cbeb0c808c3a44025872ff18e59472 100644 --- a/crates/git_ui/src/commit_view.rs +++ b/crates/git_ui/src/commit_view.rs @@ -244,10 +244,6 @@ impl language::File for GitBlob { self.worktree_id } - fn as_any(&self) -> &dyn Any { - self - } - fn to_proto(&self, _cx: &App) -> language::proto::File { unimplemented!() } @@ -282,10 +278,6 @@ impl language::File for CommitMetadataFile { self.worktree_id } - fn as_any(&self) -> &dyn Any { - self - } - fn to_proto(&self, _: &App) -> language::proto::File { unimplemented!() } diff --git a/crates/gpui/src/action.rs b/crates/gpui/src/action.rs index 783b7d47e418f3e6df32b1f0247fa7816341778b..faf0bed8faba01999c5c4c1c00f6a38796c4c6d9 100644 --- a/crates/gpui/src/action.rs +++ b/crates/gpui/src/action.rs @@ -42,13 +42,10 @@ use std::{ /// } /// register_action!(Paste); /// ``` -pub trait Action: 'static + Send { +pub trait Action: Any + Send { /// Clone the action into a new box fn boxed_clone(&self) -> Box; - /// Cast the action to the any type - fn as_any(&self) -> &dyn Any; - /// Do a partial equality check on this action and the other fn partial_eq(&self, action: &dyn Action) -> bool; @@ -94,9 +91,9 @@ impl std::fmt::Debug for dyn Action { } impl dyn Action { - /// Get the type id of this action - pub fn type_id(&self) -> TypeId { - self.as_any().type_id() + /// Type-erase Action type. + pub fn as_any(&self) -> &dyn Any { + self as &dyn Any } } @@ -557,9 +554,6 @@ macro_rules! __impl_action { ::std::boxed::Box::new(self.clone()) } - fn as_any(&self) -> &dyn ::std::any::Any { - self - } $($items)* } diff --git a/crates/gpui/src/key_dispatch.rs b/crates/gpui/src/key_dispatch.rs index cb73300a52a27da2a4b02c7cc213caf49289c9b3..110e21fbc33203dcaf44317f27d35886ad8b0533 100644 --- a/crates/gpui/src/key_dispatch.rs +++ b/crates/gpui/src/key_dispatch.rs @@ -597,10 +597,6 @@ mod tests { Box::new(TestAction) } - fn as_any(&self) -> &dyn ::std::any::Any { - self - } - fn build(_value: serde_json::Value) -> anyhow::Result> where Self: Sized, diff --git a/crates/gpui/tests/action_macros.rs b/crates/gpui/tests/action_macros.rs index 98a70daa9532ebc0e9df303338d2992406dd9ac7..5a76dfa9f3ea8e9d05579028847b93611ceb87d4 100644 --- a/crates/gpui/tests/action_macros.rs +++ b/crates/gpui/tests/action_macros.rs @@ -22,10 +22,6 @@ fn test_action_macros() { unimplemented!() } - fn as_any(&self) -> &dyn std::any::Any { - unimplemented!() - } - fn partial_eq(&self, _action: &dyn gpui::Action) -> bool { unimplemented!() } diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 5e3cb56a4974f20676d5bbed64201540fce2bfa7..fa0773a958711bbd9aa1cd05537ca37a9e4b517b 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -306,7 +306,7 @@ pub enum BufferEvent { } /// The file associated with a buffer. -pub trait File: Send + Sync { +pub trait File: Send + Sync + Any { /// Returns the [`LocalFile`] associated with this file, if the /// file is local. fn as_local(&self) -> Option<&dyn LocalFile>; @@ -336,9 +336,6 @@ pub trait File: Send + Sync { /// This is needed for looking up project-specific settings. fn worktree_id(&self, cx: &App) -> WorktreeId; - /// Converts this file into an [`Any`] trait object. - fn as_any(&self) -> &dyn Any; - /// Converts this file into a protobuf message. fn to_proto(&self, cx: &App) -> rpc::proto::File; @@ -4610,10 +4607,6 @@ impl File for TestFile { WorktreeId::from_usize(0) } - fn as_any(&self) -> &dyn std::any::Any { - unimplemented!() - } - fn to_proto(&self, _: &App) -> rpc::proto::File { unimplemented!() } diff --git a/crates/project/src/debugger/session.rs b/crates/project/src/debugger/session.rs index e6c459accc3c33ba988a81345ac1912d7ce87439..43cbe52946cb6909a0e49d3c4a32945aed5e06eb 100644 --- a/crates/project/src/debugger/session.rs +++ b/crates/project/src/debugger/session.rs @@ -746,8 +746,7 @@ pub struct Session { _background_tasks: Vec>, } -trait CacheableCommand: 'static + Send + Sync { - fn as_any(&self) -> &dyn Any; +trait CacheableCommand: Any + Send + Sync { fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool; fn dyn_hash(&self, hasher: &mut dyn Hasher); fn as_any_arc(self: Arc) -> Arc; @@ -757,12 +756,8 @@ impl CacheableCommand for T where T: DapCommand + PartialEq + Eq + Hash, { - fn as_any(&self) -> &dyn Any { - self - } - fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool { - rhs.as_any() + (rhs as &dyn Any) .downcast_ref::() .map_or(false, |rhs| self == rhs) } @@ -795,7 +790,7 @@ impl Eq for RequestSlot {} impl Hash for RequestSlot { fn hash(&self, state: &mut H) { self.0.dyn_hash(state); - self.0.as_any().type_id().hash(state) + (&*self.0 as &dyn Any).type_id().hash(state) } } @@ -1345,7 +1340,7 @@ impl Session { fn invalidate_state(&mut self, key: &RequestSlot) { self.requests - .entry(key.0.as_any().type_id()) + .entry((&*key.0 as &dyn Any).type_id()) .and_modify(|request_map| { request_map.remove(&key); }); diff --git a/crates/proto/src/typed_envelope.rs b/crates/proto/src/typed_envelope.rs index 50981373a021b4c78540306f6118c24f7acf3f52..0741b5a6109b6593ee05b87d99fc676de737ceb3 100644 --- a/crates/proto/src/typed_envelope.rs +++ b/crates/proto/src/typed_envelope.rs @@ -31,10 +31,9 @@ pub trait RequestMessage: EnvelopedMessage { type Response: EnvelopedMessage; } -pub trait AnyTypedEnvelope: 'static + Send + Sync { +pub trait AnyTypedEnvelope: Any + Send + Sync { fn payload_type_id(&self) -> TypeId; fn payload_type_name(&self) -> &'static str; - fn as_any(&self) -> &dyn Any; fn into_any(self: Box) -> Box; fn is_background(&self) -> bool; fn original_sender_id(&self) -> Option; @@ -56,10 +55,6 @@ impl AnyTypedEnvelope for TypedEnvelope { T::NAME } - fn as_any(&self) -> &dyn Any { - self - } - fn into_any(self: Box) -> Box { self } diff --git a/crates/recent_projects/src/remote_servers.rs b/crates/recent_projects/src/remote_servers.rs index 30671289efd18432ced9296725f6cd46c08f6305..61e5331cc610ae9f6cf055c318e51cc9065151fc 100644 --- a/crates/recent_projects/src/remote_servers.rs +++ b/crates/recent_projects/src/remote_servers.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::collections::BTreeSet; use std::path::PathBuf; use std::sync::Arc; @@ -1288,11 +1289,8 @@ impl RemoteServerProjects { cx.notify(); })); - let Some(scroll_handle) = scroll_state - .scroll_handle() - .as_any() - .downcast_ref::() - else { + let handle = &**scroll_state.scroll_handle() as &dyn Any; + let Some(scroll_handle) = handle.downcast_ref::() else { unreachable!() }; diff --git a/crates/rpc/src/proto_client.rs b/crates/rpc/src/proto_client.rs index e5f7b781b8db94a34ee7ea0c786d7f7a9c39e007..d01a31de85060e9ce113623be969c0f7ae7f2974 100644 --- a/crates/rpc/src/proto_client.rs +++ b/crates/rpc/src/proto_client.rs @@ -10,7 +10,7 @@ use proto::{ error::ErrorExt as _, }; use std::{ - any::TypeId, + any::{Any, TypeId}, sync::{Arc, Weak}, }; @@ -250,8 +250,7 @@ impl AnyProtoClient { let message_type_id = TypeId::of::(); let entity_type_id = TypeId::of::(); let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| { - envelope - .as_any() + (envelope as &dyn Any) .downcast_ref::>() .unwrap() .payload @@ -296,8 +295,7 @@ impl AnyProtoClient { let message_type_id = TypeId::of::(); let entity_type_id = TypeId::of::(); let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| { - envelope - .as_any() + (envelope as &dyn Any) .downcast_ref::>() .unwrap() .payload diff --git a/crates/terminal_view/src/terminal_scrollbar.rs b/crates/terminal_view/src/terminal_scrollbar.rs index 89b63d7f7bfb90f7584acf09660f1f2bceba4539..5f5546aec0c78b41a06c36d69375a5d96b03d20d 100644 --- a/crates/terminal_view/src/terminal_scrollbar.rs +++ b/crates/terminal_view/src/terminal_scrollbar.rs @@ -1,5 +1,4 @@ use std::{ - any::Any, cell::{Cell, RefCell}, rc::Rc, }; @@ -85,8 +84,4 @@ impl ScrollableHandle for TerminalScrollHandle { ), ) } - - fn as_any(&self) -> &dyn Any { - self - } } diff --git a/crates/ui/src/components/scrollbar.rs b/crates/ui/src/components/scrollbar.rs index 5756ec20cdd2ae40c92b2b8f1d3ab5605f37a215..255b5e57728947c94465b8f8a06dd9520be2e8cf 100644 --- a/crates/ui/src/components/scrollbar.rs +++ b/crates/ui/src/components/scrollbar.rs @@ -33,10 +33,6 @@ impl ScrollableHandle for UniformListScrollHandle { fn viewport(&self) -> Bounds { self.0.borrow().base_handle.bounds() } - - fn as_any(&self) -> &dyn Any { - self - } } impl ScrollableHandle for ListState { @@ -66,10 +62,6 @@ impl ScrollableHandle for ListState { fn viewport(&self) -> Bounds { self.viewport_bounds() } - - fn as_any(&self) -> &dyn Any { - self - } } impl ScrollableHandle for ScrollHandle { @@ -107,10 +99,6 @@ impl ScrollableHandle for ScrollHandle { fn viewport(&self) -> Bounds { self.bounds() } - - fn as_any(&self) -> &dyn Any { - self - } } #[derive(Debug)] @@ -119,12 +107,11 @@ pub struct ContentSize { pub scroll_adjustment: Option>, } -pub trait ScrollableHandle: Debug + 'static { +pub trait ScrollableHandle: Any + Debug { fn content_size(&self) -> Option; fn set_offset(&self, point: Point); fn offset(&self) -> Point; fn viewport(&self) -> Bounds; - fn as_any(&self) -> &dyn Any; fn drag_started(&self) {} fn drag_ended(&self) {} } diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs index 9b36c6cefca794fe2e733a6b50eb2ed4dcb23070..93e6ed25fcc5918577a8ff1b391fb77807cb8356 100644 --- a/crates/worktree/src/worktree.rs +++ b/crates/worktree/src/worktree.rs @@ -45,6 +45,7 @@ use smallvec::{SmallVec, smallvec}; use smol::channel::{self, Sender}; use std::{ any::Any, + borrow::Borrow as _, cmp::Ordering, collections::hash_map, convert::TryFrom, @@ -3263,10 +3264,6 @@ impl language::File for File { self.worktree.read(cx).id() } - fn as_any(&self) -> &dyn Any { - self - } - fn to_proto(&self, cx: &App) -> rpc::proto::File { rpc::proto::File { worktree_id: self.worktree.read(cx).id().to_proto(), @@ -3359,7 +3356,11 @@ impl File { } pub fn from_dyn(file: Option<&Arc>) -> Option<&Self> { - file.and_then(|f| f.as_any().downcast_ref()) + file.and_then(|f| { + let f: &dyn language::File = f.borrow(); + let f: &dyn Any = f; + f.downcast_ref() + }) } pub fn worktree_id(&self, cx: &App) -> WorktreeId {