Skip to content

refactor(hydro_cli)!: use direct &dyn Any upcasting for Rust 1.86, update pyo3, fix #1821 #1825

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

Closed
wants to merge 10 commits into from
23 changes: 5 additions & 18 deletions dfir_rs/src/scheduled/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ impl Dfir<'_> {
let tee_root_data_name = tee_root_data.name.clone();

// Insert new handoff output.
let teeing_handoff = tee_root_data
.handoff
.any_ref()
.downcast_ref::<TeeingHandoff<T>>()
.unwrap();
let teeing_handoff =
<dyn Any>::downcast_ref::<TeeingHandoff<T>>(&*tee_root_data.handoff).unwrap();
let new_handoff = teeing_handoff.tee();

// Handoff ID of new tee output.
Expand Down Expand Up @@ -111,11 +108,7 @@ impl Dfir<'_> {
T: Clone,
{
let data = &self.handoffs[tee_port.handoff_id];
let teeing_handoff = data
.handoff
.any_ref()
.downcast_ref::<TeeingHandoff<T>>()
.unwrap();
let teeing_handoff = <dyn Any>::downcast_ref::<TeeingHandoff<T>>(&*data.handoff).unwrap();
teeing_handoff.drop();

let tee_root = data.pred_handoffs[0];
Expand Down Expand Up @@ -875,10 +868,7 @@ impl<'a> Dfir<'a> {
.map(|hid| hid.handoff_id)
.map(|hid| handoffs.get(hid).unwrap())
.map(|h_data| {
h_data
.handoff
.any_ref()
.downcast_ref()
<dyn Any>::downcast_ref(&*h_data.handoff)
.expect("Attempted to cast handoff to wrong type.")
})
.map(RefCast::ref_cast)
Expand All @@ -889,10 +879,7 @@ impl<'a> Dfir<'a> {
.map(|hid| hid.handoff_id)
.map(|hid| handoffs.get(hid).unwrap())
.map(|h_data| {
h_data
.handoff
.any_ref()
.downcast_ref()
<dyn Any>::downcast_ref(&*h_data.handoff)
.expect("Attempted to cast handoff to wrong type.")
})
.map(RefCast::ref_cast)
Expand Down
8 changes: 5 additions & 3 deletions dfir_rs/src/scheduled/handoff/handoff_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Module for variadic handoff port lists, [`PortList`].

use std::any::Any;

use ref_cast::RefCast;
use sealed::sealed;
use variadics::{Variadic, variadic_trait};
Expand Down Expand Up @@ -95,13 +97,13 @@ where
type Ctx<'a> = (&'a PortCtx<S, H>, Rest::Ctx<'a>);
unsafe fn make_ctx<'a>(&self, handoffs: &'a SlotVec<HandoffTag, HandoffData>) -> Self::Ctx<'a> {
let (this, rest) = self;
let hoff_any = handoffs.get(this.handoff_id).unwrap().handoff.any_ref();
let hoff_any: &dyn Any = &*handoffs.get(this.handoff_id).unwrap().handoff;
debug_assert!(hoff_any.is::<H>());

let handoff = unsafe {
// SAFETY: Caller must ensure `self` is from `handoffs`.
// TODO(shadaj): replace with `downcast_ref_unchecked` when it's stabilized
&*(hoff_any as *const dyn std::any::Any as *const H)
&*(hoff_any as *const dyn Any as *const H)
};

let ctx = RefCast::ref_cast(handoff);
Expand All @@ -117,7 +119,7 @@ where
let Some(hoff_data) = handoffs.get(this.handoff_id) else {
panic!("Handoff ID {} not found in `handoffs`.", this.handoff_id);
};
let hoff_any = hoff_data.handoff.any_ref();
let hoff_any: &dyn Any = &*hoff_data.handoff;
assert!(
hoff_any.is::<H>(),
"Handoff ID {} is not of type {} in `handoffs`.",
Expand Down
10 changes: 0 additions & 10 deletions dfir_rs/src/scheduled/handoff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ pub trait CanReceive<T> {

/// A handle onto the metadata part of a [Handoff], with no element type.
pub trait HandoffMeta: Any {
/// Helper to cast an instance of `HandoffMeta` to [`Any`]. In general you cannot cast between
/// traits, including [`Any`], but this helper method works around that limitation.
///
/// For implementors: the body of this method will generally just be `{ self }`.
fn any_ref(&self) -> &dyn Any;

// TODO(justin): more fine-grained info here.
/// Return if the handoff is empty.
fn is_bottom(&self) -> bool;
Expand All @@ -42,10 +36,6 @@ impl<H> HandoffMeta for Rc<RefCell<H>>
where
H: HandoffMeta,
{
fn any_ref(&self) -> &dyn Any {
self
}

fn is_bottom(&self) -> bool {
self.borrow().is_bottom()
}
Expand Down
5 changes: 0 additions & 5 deletions dfir_rs/src/scheduled/handoff/tee.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Module for teeing handoffs, not currently used much.

use std::any::Any;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
Expand Down Expand Up @@ -71,10 +70,6 @@ where
}

impl<T> HandoffMeta for TeeingHandoff<T> {
fn any_ref(&self) -> &dyn Any {
self
}

/// If this output's buffer is empty, return true.
fn is_bottom(&self) -> bool {
self.internal.borrow().readers[self.read_from]
Expand Down
5 changes: 0 additions & 5 deletions dfir_rs/src/scheduled/handoff/vector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::{RefCell, RefMut};
use std::rc::Rc;

Expand Down Expand Up @@ -65,10 +64,6 @@ impl<T> CanReceive<Vec<T>> for VecHandoff<T> {
}

impl<T> HandoffMeta for VecHandoff<T> {
fn any_ref(&self) -> &dyn Any {
self
}

fn is_bottom(&self) -> bool {
(*self.input).borrow_mut().is_empty()
}
Expand Down
7 changes: 2 additions & 5 deletions hydro_deploy/core/src/azure.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};

Expand Down Expand Up @@ -125,10 +126,6 @@ impl Host for AzureHost {
self.id
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn collect_resources(&self, resource_batch: &mut ResourceBatch) {
if self.launched.get().is_some() {
return;
Expand Down Expand Up @@ -474,7 +471,7 @@ impl Host for AzureHost {
}
}
ClientStrategy::InternalTcpPort(target_host) => {
if let Some(provider_target) = target_host.as_any().downcast_ref::<AzureHost>() {
if let Some(provider_target) = <dyn Any>::downcast_ref::<AzureHost>(target_host) {
self.project == provider_target.project
} else {
false
Expand Down
7 changes: 1 addition & 6 deletions hydro_deploy/core/src/custom_service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::ops::Deref;
use std::sync::{Arc, OnceLock, Weak};

Expand Down Expand Up @@ -136,10 +135,6 @@ impl RustCrateSource for CustomClientPort {
}

impl RustCrateSink for CustomClientPort {
fn as_any(&self) -> &dyn Any {
self
}

fn instantiate(&self, _client_path: &SourcePath) -> Result<Box<dyn FnOnce() -> ServerConfig>> {
bail!("Custom services cannot be used as the server")
}
Expand All @@ -163,7 +158,7 @@ impl RustCrateSink for CustomClientPort {
me.downcast_ref::<CustomClientPort>()
.unwrap()
.record_server_config(client_port);
bind_type(server_host.as_any())
bind_type(&*server_host)
}))
}
}
7 changes: 2 additions & 5 deletions hydro_deploy/core/src/gcp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};

Expand Down Expand Up @@ -250,10 +251,6 @@ impl Host for GcpComputeEngineHost {
self.id
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn collect_resources(&self, resource_batch: &mut ResourceBatch) {
if self.launched.get().is_some() {
return;
Expand Down Expand Up @@ -508,7 +505,7 @@ impl Host for GcpComputeEngineHost {
}
ClientStrategy::InternalTcpPort(target_host) => {
if let Some(gcp_target) =
target_host.as_any().downcast_ref::<GcpComputeEngineHost>()
<dyn Any>::downcast_ref::<GcpComputeEngineHost>(target_host)
{
self.project == gcp_target.project
} else {
Expand Down
8 changes: 3 additions & 5 deletions hydro_deploy/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::Any;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
Expand Down Expand Up @@ -154,9 +155,9 @@ pub enum HostTargetType {
Linux,
}

pub type HostStrategyGetter = Box<dyn FnOnce(&dyn std::any::Any) -> ServerStrategy>;
pub type HostStrategyGetter = Box<dyn FnOnce(&dyn Any) -> ServerStrategy>;

pub trait Host: Send + Sync {
pub trait Host: Any + Send + Sync {
fn target_type(&self) -> HostTargetType;

fn request_port(&self, bind_type: &ServerStrategy);
Expand Down Expand Up @@ -188,9 +189,6 @@ pub trait Host: Send + Sync {

/// Determines whether this host can connect to another host using the given strategy.
fn can_connect_to(&self, typ: ClientStrategy) -> bool;

/// Returns a reference to the host as a trait object.
fn as_any(&self) -> &dyn std::any::Any;
}

#[async_trait]
Expand Down
4 changes: 0 additions & 4 deletions hydro_deploy/core/src/localhost/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ impl Host for LocalhostHost {
self.id
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn launched(&self) -> Option<Arc<dyn LaunchedHost>> {
Some(Arc::new(LaunchedLocalhost))
}
Expand Down
24 changes: 5 additions & 19 deletions hydro_deploy/core/src/rust_crate/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait RustCrateSource: Send + Sync {
self.wrap_reverse_server_config(p)
})
.unwrap();
self.record_server_strategy(instantiated(sink.as_any()));
self.record_server_strategy(instantiated(sink));
}
}
}
Expand All @@ -48,9 +48,7 @@ pub trait RustCrateServer: DynClone + Send + Sync {

pub type ReverseSinkInstantiator = Box<dyn FnOnce(&dyn Any) -> ServerStrategy>;

pub trait RustCrateSink: Send + Sync {
fn as_any(&self) -> &dyn Any;

pub trait RustCrateSink: Any + Send + Sync {
/// Instantiate the sink as the source host connecting to the sink host.
/// Returns a thunk that can be called to perform mutations that instantiate the sink.
fn instantiate(&self, client_path: &SourcePath) -> Result<Box<dyn FnOnce() -> ServerConfig>>;
Expand Down Expand Up @@ -116,10 +114,6 @@ impl RustCrateSource for NullSourceSink {
}

impl RustCrateSink for NullSourceSink {
fn as_any(&self) -> &dyn Any {
self
}

fn instantiate(&self, _client_path: &SourcePath) -> Result<Box<dyn FnOnce() -> ServerConfig>> {
Ok(Box::new(|| ServerConfig::Null))
}
Expand All @@ -139,10 +133,6 @@ pub struct DemuxSink {
}

impl RustCrateSink for DemuxSink {
fn as_any(&self) -> &dyn Any {
self
}

fn instantiate(&self, client_host: &SourcePath) -> Result<Box<dyn FnOnce() -> ServerConfig>> {
let mut thunk_map = HashMap::new();
for (key, target) in &self.demux {
Expand Down Expand Up @@ -182,7 +172,7 @@ impl RustCrateSink for DemuxSink {
let me = me.downcast_ref::<DemuxSink>().unwrap();
let instantiated_map = thunk_map
.into_iter()
.map(|(key, thunk)| (key, thunk(me.demux.get(&key).unwrap().as_any())))
.map(|(key, thunk)| (key, (thunk)(me.demux.get(&key).unwrap())))
.collect();

ServerStrategy::Demux(instantiated_map)
Expand Down Expand Up @@ -314,10 +304,6 @@ impl SourcePath {
}

impl RustCrateSink for RustCratePortConfig {
fn as_any(&self) -> &dyn Any {
self
}

fn instantiate(&self, client_path: &SourcePath) -> Result<Box<dyn FnOnce() -> ServerConfig>> {
let server = self.service.upgrade().unwrap();
let server_read = server.try_read().unwrap();
Expand All @@ -331,7 +317,7 @@ impl RustCrateSink for RustCratePortConfig {
let port = self.port.clone();
Ok(Box::new(move || {
let mut server_write = server.try_write().unwrap();
let bind_type = (bind_type)(server_write.on.as_any());
let bind_type = (bind_type)(&*server_write.on);

if merge {
let merge_config = server_write
Expand Down Expand Up @@ -392,7 +378,7 @@ impl RustCrateSink for RustCratePortConfig {
.insert(port.clone(), client_port);
};

(bind_type)(client_write.on.as_any())
(bind_type)(&*client_write.on)
}))
}
}
Expand Down
3 changes: 1 addition & 2 deletions hydro_deploy/core/src/rust_crate/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ impl RustCrateService {
)?;

assert!(!self.port_to_bind.contains_key(&my_port));
self.port_to_bind
.insert(my_port, instantiated(sink.as_any()));
self.port_to_bind.insert(my_port, (instantiated)(sink));

Ok(())
}
Expand Down
Loading