Arc Downcast Alias

I want to alias std::sync::Arc::downcast for a trait:

use std::{any::Any, sync::Arc};

trait Node {
	fn to<T: Any + Sync + Send>(self: Arc<Self>) -> Arc<T> where Self: 'static, Self: Send, Self: Sync, Self: Sized {
	    Arc::downcast::<T>(self).unwrap()
	}
}

So this enables typing something like node.to::<Bitmap>(). It works, but as you can see I was obligged to add a lot of bounds manually to Self. Is there a way to simplify that?

P.S.: although that verbosity doesn't matter at all since in regards to subtype conversions I'll only provide to and is.

At a minimum, you could simplify the way you write the bounds:

	fn to<T: Any + Sync + Send>(self: Arc<Self>) -> Arc<T>
    where
        Self: 'static + Send + Sync + Sized {

This is the same set of bounds, in the same order, but expressed in a shorter form.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.