简体   繁体   中英

Rust From trait abuse?

Let's say I have an object struct that I want to create From config struct, that seems reasonable - that is a conversion from one struct type to another.

Then let's say that I want to create the latter config struct from Path ?

Meaning from trait implementation taking Path as the parameter, opening the YAML file, parsing it, and returning the config struct.

How much abuse will that be, or will it be a completely normal and acceptable thing to do?

I've definitely seen the first usage case of traits in the oss code, but haven't seen the latter one.

Meaning from trait implementation taking Path as the parameter, opening yaml file, parsing it, and returning config struct.

How much of an abuse will that be, or will it be completely normal and acceptable thing to do?

It's a significant abuse:

  1. The entire process is rife with points of failure, but From is non-failable .

  2. From and TryFrom are both designed for conversions . Your use-case has nothing to do with conversion, you can't call that converting a path to a configuration object without making the term completely meaningless .

And at a fundamental level... it doesn't really make sense? The point of From / TryFrom is to serve as generic bounds so that eg a function can take anything which is convertible to / from a concrete type it knows. How useful is an Into<Config> bound going to be? I'm guessing "not very".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM