Why must TryInto consume? (The Refactoring Blues)

Scenario: I want to convert a vendor-specific "message" type (call it Message) to a neutral domain message type.

I implement TryFrom - idiomatic Rust.

A function take a &Message and calls try_into with it.

Does not compile - try_into consumes its value.

Of course, I can clone the message. But I don't want to - they are reasonably tight, but the system may process thousands per second.

Here's where I'm going with this: I can get around using a function that takes &Message by inlining that function into the parent function that creates the Messages (from a gRPC stream serving JSON). But that's ugly!

I do not want a language to dictate how I arrange & refactoring functional units. But I find myself feeling that Rust does just that.

Am I missing something?

1 Like

You can do impl<'a> TryFrom<&'a Message> for OtherMessage. Or just make your function take an owned Message.

7 Likes

Another facepalm moment for me!