CosmWasm / cosmwasm

Framework for building smart contracts in Wasm for the Cosmos SDK
https://www.cosmwasm.com/
Apache License 2.0
1.07k stars 334 forks source link

Provide function for downcasting trait to concrete type for better test implementation. #1375

Open y-pakorn opened 2 years ago

y-pakorn commented 2 years ago

Is it possible to add a downcast function to traits like Api, Storage, and Querier? So that custom implementation of those traits can be cast back to the original type.

I'm running into problems trying to use my custom storage with gas meter for testing and can't cast back to my original type to use the function since the dyn Storage is used everywhere in cw-multi-test.

Something like this (and maybe enforce to be used only in test target) would be nice?

pub trait Storage {
  // ...

  fn as_any(&self) -> &dyn Any;
}
y-pakorn commented 2 years ago

The workaround, for now, is to mutate the inner struct through interior mutability and pass a pointer as a trait object instead.

webmaster128 commented 2 years ago

I don't fully understand the issue. Isn't MemoryStorageWithGas compatible to dyn Storage because of impl Storage for MemoryStorageWithGas?

ethanfrey commented 1 year ago

Maybe you can raise this issue on the new cw-multi-test repo and figure out your problem and any possible solutions there before we change the standard lib. This is much more stable and cw-multi-test a lot more experimental / flexible

chipshort commented 1 month ago

This will also help with the remaining point in #2157