Closed crackcomm closed 3 years ago
Yes makes more sense probably. Though I am not sure, is there any case were you shouldn't use impl From
or is it okay to use for all conversions?
Usage of impl From
should be limited to use cases that are conversions between types.
Traits like ToString
or FromStr
should be implemented in cases of deserialization/serialization when input can be invalid but output is always possible.
In this case it seems perfectly fine and more valid to have:
impl From<Ipv4Addr> for Multiaddr
instead of
impl ToMultiaddr for Ipv4Addr
because as such it is only a type conversion.
Thanks for the explanation.
Note that this should be TryFrom
and not just From
, because the conversion can return an error.
So I'd love to contribute to this project with the caveat that I'm coming from a c++/scala/haskell background and trying to learn rust so there might be a vocabulary gap. That said I have a couple of questions around the context of this issue:
From
, or is it TryFrom
and now the library requires a nightly compiler?TryFrom
for each protocol to ensure a homogeneous interface, by TryFrom
instances for the types which might fail and From
instances for those types which are assured of success, or by waiting until TryFrom
becomes stable?From/TryFrom
instances target, for example, impl<'a> From<&'a SocketAddr> for Multiaddr
or impl From<SocketAddr> for MultiAddr
?Closing here since this is addressed with https://github.com/multiformats/rust-multiaddr/pull/40.
@jcc333 let us know in case you would still like to contribute.
It seems to me that there is too much of
to_*
andfrom_*
when it should be enough to use.into()
and singleimpl From
.