gear
gear copied to clipboard
Refactor async functions in gstd
File Location(s)
https://github.com/gear-tech/gear/blob/master/gstd/src/msg/async.rs
Proposal
- we have both
CodecMessageFutureandMessageFuturewith only 1 line difference - the logic of the async functions are the same, interrupted and waiting for the reply of the returned
MessageId - really a lot of duplicated code, see my redundant implementations https://github.com/gear-tech/gear/pull/1037
we can just use two wrappers to implement this
async fn wait_for_reply(id: MessageId) -> Result<Vec<u8>>;
async fn wait_for_reply_as<D: Decode>(id: MessageId) -> Result<D>;
or
impl MessageId {
async fn for_reply() -> Result<Vec<u8>>;
async fn for_reply_as<D: Decode>() -> Result<D>;
}
or
trait WaitForReply {
async fn for_reply() -> Result<Vec<u8>>;
}
trait WaitForReplyAs {
async fn for_reply_as() -> Result<Vec<u8>>;
}
impl WaitForReply for MessageId {}
impl WaitForReplyAs for MessageId {}
As discussed before, this idea of the trait seems beauty, but we don’t need to provide ability to implement that trait for some other structs. The methods on message id isn’t the best solution, cause user can call it on custom message id. I these cases we should prevent. So the suggested solution seems unreachable for me, but there is a place for other similar considering option to reduce code size.
Solved by auto-generated fns