Storing arbitrary Fn in Any

I have an Any and I’d like to put Fn(&mut Cow<str>) in it. How can I do this?

You need to give more information thann that, how are you storing your Any and Fn?

let my_any: Any = my_fn.into();?

Any is unsized so this isn’t possible to use it on the stack directly, same with FnMut.

True, I wrote it to quickly. My point was, that if you have FnMut, you simply into it into Any.

But you can’t because that would require an impl of Into<dyn Any> for dyn FnMut, which doesn’t exist

I have:

type Handlers<T> = Arc<Vec<RwLock<Deeply<Nested<dyn for<'a> Fn(&'a mut T)>>>>>;
type HandlerHolder = AnyMap;

and I would like to put a Handlers<Cow<str>> in the AnyMap.

He never told, he have dyn FnMut, I understand him, that he has some actuall function (probably closure, but thats only guess), so it would be some unspecified type, which is impl FnMut. But this actually requires clarification.

I’m a they

Ah, ok

Could you please post the actual type.

sorry I got it slightly wrong: https://cybre.tech/SoniEx2/rust.eventbus/src/branch/master/src/lib.rs#L206

Shouldn’t the RwLock be inside of the Arc, otherwise there is no reason to have it.

struct Handlers<T: Event + ?Sized>(Vec<Arc<RwLock<(i32, Arc<dyn Fn(&mut T) + Send + Sync>)>>>);

I can assure you that this is correct.

How does that work. The outer RwLock adds zero value, and actively makes it harder to read and write code.

The point is that you can clone the Arc and make_mut the original. It doesn’t lock the whole thing just to modify the innermost elements.

I don’t think you can convert trait objects to other trait objects, because there is no way guarantee to know how to construct the fat pointer. For example,

&dyn Debug cannot be converted to &dyn Any, because you can’t recover the pointers to the functions in Any from a reference to Debug. The conversions that Any does are special cased.

what about Arc? what about Vec<Arc>? I think the problem of turning &dyn Foo into &dyn Any is that it isn’t 'static, not the vtable.

Is this a problem for you to store Arc<dyn Fn(&mut T)>> in your AnyMap ? It is 'static , so it shouldn’t be a problem.

I want T to be something like Foo<'a>.