简体   繁体   English

关联类型的实例上的模式匹配困难

[英]Difficulty with pattern matching on an instance of an associated type

I am relying on rustc_serialize to parse JSON data into a struct, Orders , which represents a Vec of Order structs. 我依靠rustc_serialize将JSON数据解析为结构Orders ,该结构表示Order结构的Vec The JSON data may have an array or a null value; JSON数据可以具有数组或null值; my intent is to either parse the array of orders normally, if any, or parse the null value as an Orders with an empty Vec . 我的意图是正常解析订单数组(如果有),或者将null值解析为具有空VecOrders If neither of these is the case, then an error is to be relayed. 如果以上两种情况均不可行,则将传递一个错误。 This is my attempt: 这是我的尝试:

impl Decodable for Orders {
    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
        let try = d.read_seq(|d, l| {
            let mut orders = Vec::new();
            for _ in 0..l {
                let order = try!(Decodable::decode(d));
                orders.push(order);
            }
            Ok(Orders(orders))
        });

        match try {
            value @ Ok(_) => value,
            error @ Err(e) => match e {
                ExpectedError(_, x) if &x == "null" => Ok(Orders(Vec::new())),
                _ => error,
            },
        }
    } 
}

My issue has to do with pattern matching on ExpectedError . 我的问题与ExpectedError上的模式匹配有关。 The compiler gives me the following error: 编译器给我以下错误:

 expected `<D as rustc_serialize::serialize::Decoder>::Error`,
    found `rustc_serialize::json::DecoderError`
(expected associated type,
    found enum `rustc_serialize::json::DecoderError`) [E0308]
src/api/types/json.rs:125                 ExpectedError(_, x) if &x == "null" => Ok(Orders(Vec::new())),
                                          ^~~~~~~~~~~~~~~~~~~

I am stumped on this one. 我为此感到困惑。 How can I correct this? 我该如何纠正?

How can I correct this? 我该如何纠正?

In general, you would have to choose between being generic or specialized. 通常,您必须在通用或专业之间进行选择。 You cannot pattern match on an associated type because this type can be anything and a generic method should work for any type which satisfies the constraints. 您不能对关联类型进行模式匹配,因为该类型可以是任何类型,并且通用方法应适用于满足约束的任何类型。

For example, in your case: 例如,在您的情况下:

  • <D as rustc_serialize::serialize::Decoder>::Error can be anything <D as rustc_serialize::serialize::Decoder>::Error可能是任何东西
  • rustc_serialize::json::DecoderError is but one possibility rustc_serialize::json::DecoderError只是一种可能

So you should normally choose between using some abstract D or specializing the decoding for json . 因此,通常应在使用一些抽象D或对json专门解码之间进行选择。


However, since you are implementing Decodable here, you cannot choose NOT to be generic as you do not get to pick the signature. 但是,由于要在此处实现“可Decodable ,因此您不能选择“不通用”,因为您不会选择签名。

Furthermore, it appears that rustc_serialize does not support tentative parsing (or at least, its json implementation does not), so I am afraid that you cannot check for nullity (using d.read_nil() ) beforehand. 此外,似乎rustc_serialize不支持临时解析(或者至少不支持其json实现),因此,恐怕您无法事先检查无效性(使用d.read_nil() )。

I suppose those limitations can be seen as the reason that this library is being retired in favor of serde , and can only encourage you to try it out. 我想这些限制可以看作是该库被Serde淘汰的原因 ,并且只能鼓励您尝试一下。

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

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