chore: Replace as_any functions with trait upcasting (#28221)

Closes #ISSUE

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2025-04-08 22:16:27 +02:00 committed by GitHub
parent 38ec45008c
commit 0b75c13034
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 23 additions and 87 deletions

View file

@ -1280,10 +1280,6 @@ mod tests {
unimplemented!() unimplemented!()
} }
fn as_any(&self) -> &dyn std::any::Any {
unimplemented!()
}
fn to_proto(&self, _: &App) -> rpc::proto::File { fn to_proto(&self, _: &App) -> rpc::proto::File {
unimplemented!() unimplemented!()
} }

View file

@ -244,10 +244,6 @@ impl language::File for GitBlob {
self.worktree_id self.worktree_id
} }
fn as_any(&self) -> &dyn Any {
self
}
fn to_proto(&self, _cx: &App) -> language::proto::File { fn to_proto(&self, _cx: &App) -> language::proto::File {
unimplemented!() unimplemented!()
} }
@ -282,10 +278,6 @@ impl language::File for CommitMetadataFile {
self.worktree_id self.worktree_id
} }
fn as_any(&self) -> &dyn Any {
self
}
fn to_proto(&self, _: &App) -> language::proto::File { fn to_proto(&self, _: &App) -> language::proto::File {
unimplemented!() unimplemented!()
} }

View file

@ -42,13 +42,10 @@ use std::{
/// } /// }
/// register_action!(Paste); /// register_action!(Paste);
/// ``` /// ```
pub trait Action: 'static + Send { pub trait Action: Any + Send {
/// Clone the action into a new box /// Clone the action into a new box
fn boxed_clone(&self) -> Box<dyn Action>; fn boxed_clone(&self) -> Box<dyn Action>;
/// Cast the action to the any type
fn as_any(&self) -> &dyn Any;
/// Do a partial equality check on this action and the other /// Do a partial equality check on this action and the other
fn partial_eq(&self, action: &dyn Action) -> bool; fn partial_eq(&self, action: &dyn Action) -> bool;
@ -94,9 +91,9 @@ impl std::fmt::Debug for dyn Action {
} }
impl dyn Action { impl dyn Action {
/// Get the type id of this action /// Type-erase Action type.
pub fn type_id(&self) -> TypeId { pub fn as_any(&self) -> &dyn Any {
self.as_any().type_id() self as &dyn Any
} }
} }
@ -557,9 +554,6 @@ macro_rules! __impl_action {
::std::boxed::Box::new(self.clone()) ::std::boxed::Box::new(self.clone())
} }
fn as_any(&self) -> &dyn ::std::any::Any {
self
}
$($items)* $($items)*
} }

View file

@ -597,10 +597,6 @@ mod tests {
Box::new(TestAction) Box::new(TestAction)
} }
fn as_any(&self) -> &dyn ::std::any::Any {
self
}
fn build(_value: serde_json::Value) -> anyhow::Result<Box<dyn Action>> fn build(_value: serde_json::Value) -> anyhow::Result<Box<dyn Action>>
where where
Self: Sized, Self: Sized,

View file

@ -22,10 +22,6 @@ fn test_action_macros() {
unimplemented!() unimplemented!()
} }
fn as_any(&self) -> &dyn std::any::Any {
unimplemented!()
}
fn partial_eq(&self, _action: &dyn gpui::Action) -> bool { fn partial_eq(&self, _action: &dyn gpui::Action) -> bool {
unimplemented!() unimplemented!()
} }

View file

@ -306,7 +306,7 @@ pub enum BufferEvent {
} }
/// The file associated with a buffer. /// 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 /// Returns the [`LocalFile`] associated with this file, if the
/// file is local. /// file is local.
fn as_local(&self) -> Option<&dyn LocalFile>; 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. /// This is needed for looking up project-specific settings.
fn worktree_id(&self, cx: &App) -> WorktreeId; 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. /// Converts this file into a protobuf message.
fn to_proto(&self, cx: &App) -> rpc::proto::File; fn to_proto(&self, cx: &App) -> rpc::proto::File;
@ -4610,10 +4607,6 @@ impl File for TestFile {
WorktreeId::from_usize(0) WorktreeId::from_usize(0)
} }
fn as_any(&self) -> &dyn std::any::Any {
unimplemented!()
}
fn to_proto(&self, _: &App) -> rpc::proto::File { fn to_proto(&self, _: &App) -> rpc::proto::File {
unimplemented!() unimplemented!()
} }

View file

@ -746,8 +746,7 @@ pub struct Session {
_background_tasks: Vec<Task<()>>, _background_tasks: Vec<Task<()>>,
} }
trait CacheableCommand: 'static + Send + Sync { trait CacheableCommand: Any + Send + Sync {
fn as_any(&self) -> &dyn Any;
fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool; fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool;
fn dyn_hash(&self, hasher: &mut dyn Hasher); fn dyn_hash(&self, hasher: &mut dyn Hasher);
fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>; fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
@ -757,12 +756,8 @@ impl<T> CacheableCommand for T
where where
T: DapCommand + PartialEq + Eq + Hash, T: DapCommand + PartialEq + Eq + Hash,
{ {
fn as_any(&self) -> &dyn Any {
self
}
fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool { fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool {
rhs.as_any() (rhs as &dyn Any)
.downcast_ref::<Self>() .downcast_ref::<Self>()
.map_or(false, |rhs| self == rhs) .map_or(false, |rhs| self == rhs)
} }
@ -795,7 +790,7 @@ impl Eq for RequestSlot {}
impl Hash for RequestSlot { impl Hash for RequestSlot {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.dyn_hash(state); 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) { fn invalidate_state(&mut self, key: &RequestSlot) {
self.requests self.requests
.entry(key.0.as_any().type_id()) .entry((&*key.0 as &dyn Any).type_id())
.and_modify(|request_map| { .and_modify(|request_map| {
request_map.remove(&key); request_map.remove(&key);
}); });

View file

@ -31,10 +31,9 @@ pub trait RequestMessage: EnvelopedMessage {
type Response: 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_id(&self) -> TypeId;
fn payload_type_name(&self) -> &'static str; fn payload_type_name(&self) -> &'static str;
fn as_any(&self) -> &dyn Any;
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync>; fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync>;
fn is_background(&self) -> bool; fn is_background(&self) -> bool;
fn original_sender_id(&self) -> Option<PeerId>; fn original_sender_id(&self) -> Option<PeerId>;
@ -56,10 +55,6 @@ impl<T: EnvelopedMessage> AnyTypedEnvelope for TypedEnvelope<T> {
T::NAME T::NAME
} }
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> { fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> {
self self
} }

View file

@ -1,3 +1,4 @@
use std::any::Any;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -1288,11 +1289,8 @@ impl RemoteServerProjects {
cx.notify(); cx.notify();
})); }));
let Some(scroll_handle) = scroll_state let handle = &**scroll_state.scroll_handle() as &dyn Any;
.scroll_handle() let Some(scroll_handle) = handle.downcast_ref::<ScrollHandle>() else {
.as_any()
.downcast_ref::<ScrollHandle>()
else {
unreachable!() unreachable!()
}; };

View file

@ -10,7 +10,7 @@ use proto::{
error::ErrorExt as _, error::ErrorExt as _,
}; };
use std::{ use std::{
any::TypeId, any::{Any, TypeId},
sync::{Arc, Weak}, sync::{Arc, Weak},
}; };
@ -250,8 +250,7 @@ impl AnyProtoClient {
let message_type_id = TypeId::of::<M>(); let message_type_id = TypeId::of::<M>();
let entity_type_id = TypeId::of::<E>(); let entity_type_id = TypeId::of::<E>();
let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| { let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| {
envelope (envelope as &dyn Any)
.as_any()
.downcast_ref::<TypedEnvelope<M>>() .downcast_ref::<TypedEnvelope<M>>()
.unwrap() .unwrap()
.payload .payload
@ -296,8 +295,7 @@ impl AnyProtoClient {
let message_type_id = TypeId::of::<M>(); let message_type_id = TypeId::of::<M>();
let entity_type_id = TypeId::of::<E>(); let entity_type_id = TypeId::of::<E>();
let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| { let entity_id_extractor = |envelope: &dyn AnyTypedEnvelope| {
envelope (envelope as &dyn Any)
.as_any()
.downcast_ref::<TypedEnvelope<M>>() .downcast_ref::<TypedEnvelope<M>>()
.unwrap() .unwrap()
.payload .payload

View file

@ -1,5 +1,4 @@
use std::{ use std::{
any::Any,
cell::{Cell, RefCell}, cell::{Cell, RefCell},
rc::Rc, rc::Rc,
}; };
@ -85,8 +84,4 @@ impl ScrollableHandle for TerminalScrollHandle {
), ),
) )
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

View file

@ -33,10 +33,6 @@ impl ScrollableHandle for UniformListScrollHandle {
fn viewport(&self) -> Bounds<Pixels> { fn viewport(&self) -> Bounds<Pixels> {
self.0.borrow().base_handle.bounds() self.0.borrow().base_handle.bounds()
} }
fn as_any(&self) -> &dyn Any {
self
}
} }
impl ScrollableHandle for ListState { impl ScrollableHandle for ListState {
@ -66,10 +62,6 @@ impl ScrollableHandle for ListState {
fn viewport(&self) -> Bounds<Pixels> { fn viewport(&self) -> Bounds<Pixels> {
self.viewport_bounds() self.viewport_bounds()
} }
fn as_any(&self) -> &dyn Any {
self
}
} }
impl ScrollableHandle for ScrollHandle { impl ScrollableHandle for ScrollHandle {
@ -107,10 +99,6 @@ impl ScrollableHandle for ScrollHandle {
fn viewport(&self) -> Bounds<Pixels> { fn viewport(&self) -> Bounds<Pixels> {
self.bounds() self.bounds()
} }
fn as_any(&self) -> &dyn Any {
self
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -119,12 +107,11 @@ pub struct ContentSize {
pub scroll_adjustment: Option<Point<Pixels>>, pub scroll_adjustment: Option<Point<Pixels>>,
} }
pub trait ScrollableHandle: Debug + 'static { pub trait ScrollableHandle: Any + Debug {
fn content_size(&self) -> Option<ContentSize>; fn content_size(&self) -> Option<ContentSize>;
fn set_offset(&self, point: Point<Pixels>); fn set_offset(&self, point: Point<Pixels>);
fn offset(&self) -> Point<Pixels>; fn offset(&self) -> Point<Pixels>;
fn viewport(&self) -> Bounds<Pixels>; fn viewport(&self) -> Bounds<Pixels>;
fn as_any(&self) -> &dyn Any;
fn drag_started(&self) {} fn drag_started(&self) {}
fn drag_ended(&self) {} fn drag_ended(&self) {}
} }

View file

@ -45,6 +45,7 @@ use smallvec::{SmallVec, smallvec};
use smol::channel::{self, Sender}; use smol::channel::{self, Sender};
use std::{ use std::{
any::Any, any::Any,
borrow::Borrow as _,
cmp::Ordering, cmp::Ordering,
collections::hash_map, collections::hash_map,
convert::TryFrom, convert::TryFrom,
@ -3263,10 +3264,6 @@ impl language::File for File {
self.worktree.read(cx).id() self.worktree.read(cx).id()
} }
fn as_any(&self) -> &dyn Any {
self
}
fn to_proto(&self, cx: &App) -> rpc::proto::File { fn to_proto(&self, cx: &App) -> rpc::proto::File {
rpc::proto::File { rpc::proto::File {
worktree_id: self.worktree.read(cx).id().to_proto(), worktree_id: self.worktree.read(cx).id().to_proto(),
@ -3359,7 +3356,11 @@ impl File {
} }
pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> Option<&Self> { pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> 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 { pub fn worktree_id(&self, cx: &App) -> WorktreeId {