wokket / rust-hl7

Learning rust by playing with a HL7 parser. Use for real at your own risk!
19 stars 10 forks source link

Feature request: implement std::clone::Clone for Message #8

Closed sempervictus closed 3 years ago

sempervictus commented 3 years ago

A number of functions break when Clone isn't implemented for a Type, including things like to_owned() due to:

the method `to_owned` exists for struct `rusthl7::message::Message<'_>`, but its trait bounds were not satisfied

method cannot be called on `rusthl7::message::Message<'_>` due to unsatisfied trait bounds

note: the following trait bounds were not satisfied:
      `rusthl7::message::Message<'_>: std::clone::Clone`
      which is required by `rusthl7::message::Message<'_>: std::borrow::ToOwned`rustc(E0599)
message.rs(9, 1): doesn't satisfy `_: std::borrow::ToOwned`
message.rs(9, 1): doesn't satisfy `rusthl7::message::Message<'_>: std::clone::Clone`
main.rs(53, 80): method cannot be called on `rusthl7::message::Message<'_>` due to unsatisfied trait bounds

Now that we can extract pieces of messages as owned elements, i think we can leverage that to clone new messages from existing ones by extracting to a new object. Thoughts?

wokket commented 3 years ago

Implementing idiomatic traits like clone is definately something I'm keen to do even though I'll have to go read up on what's involved.

I'm curious to see if we're better off cloning to a new object by cloning the underlying data (eg clone segments, each of which impl's clone by cloning it's fields), or simply cloning the source message String and generating a whole new Message by calling parse on the cloned hl7 string and returning that...

Of course if it's as simple as a derive...

sempervictus commented 3 years ago

Check the PR - clone() and subsequently to_owned() now work :)

sempervictus commented 3 years ago

Closing for 957c3a65803964a7215e14a3a3abf596a1ad928e