如何解决如何将成功组合器与 nom 中的地图组合器结合使用?
我正在编写一个消耗字节并返回 Option<&[u8]>
的 nom 解析器组合器。解析器组合器应遵循以下规则:
- 读取单个有符号大端 16 位整数 (
s
) - 如果
s
为 -1,则返回None
- 如果
s
不是 -1,读取s
位并返回Some
这是我的解析器组合器:
fn parse(i: &[u8]) -> nom::IResult<&[u8],Option<&[u8]>> {
nom::combinator::flat_map(be_i16,|s: i16| {
match s {
-1 => nom::combinator::success(None),_ => nom::combinator::map(take(s as u16),Some)
}
})(i)
}
但是我看到以下错误:
error[E0308]: `match` arms have incompatible types
--> src/main.rs:15:18
|
13 | / match s {
14 | | -1 => nom::combinator::success(None),| | ------------------------------ this is found to be of type `impl Fn<(_,)>`
15 | | _ => nom::combinator::map(take(s as u16),Some)
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type,found a different opaque type
16 | | }
| |_________- `match` arms have incompatible types
|
::: /Users/dfarr/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-6.0.1/src/combinator/mod.rs:74:64
|
74 | pub fn map<I,O1,O2,E,F,G>(mut first: F,mut second: G) -> impl FnMut(I) -> IResult<I,E>
| ---------------------------------- the found opaque type
|
= note: expected type `impl Fn<(_,)>`
found opaque type `impl FnMut<(_,)>`
= note: distinct uses of `impl Trait` result in different opaque types
我可以看到 nom::combinator::success
确实具有 impl Fn(I) -> ...
的返回类型,而 nom::combinator::map
返回 impl FnMut(I) -> ...
。是否有更惯用的使用 nom 的方式,这些组合器可以以这种方式一起使用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。