`trait Checksumable` VS `impl From<&Foo> for Digest`

This post contains two questions :

Is there a Checksum trait or something like that in standard library, so I can implement my structs with it ?

Second question is doing stuff like impl From<&Foo> for Digest considered a bad practice ? If yes, is there a From or Into analogue which is meant for a factory-like feature instead of conversion ?

Thanks for your help

What should a checksum trait do? There's std::hash::Hash, but it might not be what you want.

Again, without knowing exactly what Digest is, it's hard to give a definitive answer. In general, for direct, infallible conversions, From/Into is used, like impl From<&str> for String; for more constructions (which I assume is what you mean by "factory-like"), dedicated methods are provided, like std::str::from_utf8.

I'd say yes. From/Into are typically lossless conversions between different representations of the same data. Conversion to Digest is lossy, and it's a different kind of data.

Your traits can add methods to existing objects, so you can just add .digest() method to everything.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.