gear icon indicating copy to clipboard operation
gear copied to clipboard

Refactor async functions in gstd

Open clearloop opened this issue 3 years ago • 1 comments

File Location(s)

https://github.com/gear-tech/gear/blob/master/gstd/src/msg/async.rs

Proposal

  1. we have both CodecMessageFuture and MessageFuture with only 1 line difference
  2. the logic of the async functions are the same, interrupted and waiting for the reply of the returned MessageId
  3. 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 {}

clearloop avatar Jun 10 '22 10:06 clearloop

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.

breathx avatar Jun 13 '22 13:06 breathx

Solved by auto-generated fns

breathx avatar Oct 05 '22 01:10 breathx