简体   繁体   English

Rust 来自性状滥用?

[英]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.假设我有一个object结构,我想配置结构创建,这似乎是合理的——这是从一种结构类型到另一种结构类型的转换。

Then let's say that I want to create the latter config struct from Path ?然后假设我想从Path创建后一个配置结构?

Meaning from trait implementation taking Path as the parameter, opening the YAML file, parsing it, and returning the config struct.以 Path 作为参数的 trait 实现的含义,打开 YAML 文件,解析它,并返回配置结构。

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.我肯定在 oss 代码中看到了第一个特征的使用案例,但没有看到后一个。

Meaning from trait implementation taking Path as the parameter, opening yaml file, parsing it, and returning config struct.以 Path 作为参数的 trait 实现的含义,打开 yaml 文件,解析它,并返回配置结构。

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 .整个过程充满了失败点,但Fromnon-failable

  2. From and TryFrom are both designed for conversions . FromTryFrom都是为转换而设计的。 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 .您的用例与转换无关,您不能称将路径转换为配置 object 而不使该术语完全没有意义

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. From / TryFrom的要点是用作通用边界,以便例如 function 可以采用任何可转换为/从它知道的具体类型转换的东西。 How useful is an Into<Config> bound going to be? Into<Config>绑定会有多大用处? I'm guessing "not very".我猜“不是很”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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