gpt4 book ai didi

rust - 在标称5.1.2中使用位解析器时找不到正确的类型参数

转载 作者:行者123 更新时间:2023-12-03 11:39:22 28 4
gpt4 key购买 nike

我在nom 5中找不到正确的语法来进行位解析。
我正在尝试:

pub fn parse_normal_record_header(i: &[u8]) -> nom::IResult<&[u8], FitRecordHeader> {

let (i, _) = nom::bits::bits(nom::bits::complete::tag(0x0, 1_usize))(i)?;
...
}
并从编译器获取此信息:
error[E0283]: type annotations needed
--> src/fitparsers.rs:658:18
|
658 | let (i, _) = nom::bits::bits(nom::bits::complete::tag(0x0, 1_usize))(i)?;
| ^^^^^^^^^^^^^^^ cannot infer type for type parameter `E1` declared on the function `bits`
|
::: /Users/djk/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-5.1.2/src/bits/mod.rs:37:23
|
37 | pub fn bits<I, O, E1: ParseError<(I, usize)>+ErrorConvert<E2>, E2: ParseError<I>, P>(parser: P) -> impl Fn(I) -> IResult<I, O, E2>
| ---------------------- required by this bound in `nom::bits`
|
= note: cannot satisfy `_: nom::error::ParseError<(&[u8], usize)>`
help: consider specifying the type arguments in the function call
|
658 | let (i, _) = nom::bits::bits::<I, O, E1, E2, P>(nom::bits::complete::tag(0x0, 1_usize))(i)?;
我已经尝试过各种可怕的事情,例如:
let (i, _) = nom::bits::bits(nom::bits::complete::tag::<&[u8], (&[u8], u8), usize, dyn nom::error::ParseError<(&[u8], usize)>>(0, 1_usize))(i)?;
...试图遵循我在nom来源中看到的内容,但这会产生不同的错误:
error[E0277]: the size for values of type `dyn nom::error::ParseError<(&[u8], usize)>` cannot be known at compilation time
--> src/fitparsers.rs:662:34
|
662 | ...om::bits::bits(nom::bits::complete::tag::<&[u8], (&[u8], u8), usize, dyn nom::error::ParseError<(&[u8], usize)>>(0, 1_usize))(i)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: /Users/djk/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-5.1.2/src/bits/complete.rs:57:21
|
57 | pub fn tag<I, O, C, E: ParseError<(I, usize)>>(pattern: O, count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
| - required by this bound in `nom::complete::tag`
|
= help: the trait `std::marker::Sized` is not implemented for `dyn nom::error::ParseError<(&[u8], usize)>`
我究竟做错了什么?

最佳答案

这是一个已知的问题。下面的注释就足够了:

nom::bits::complete::tag::<_, _, _, (_, _)>(0x0, 1_usize)
但是,请注意,您可能在这里犯了另一个错误。是的, bits组合器将 nom切换到位模式,但仅在内部解析器的持续时间内。如果不完整,则在退出时丢弃该字节的剩余部分。因此,使用 bits的正常方法是
nom::bits::bits(nom::sequence::tuple::<_, _, (_, _), _>((
/* a number of bitfields that add up to a whole number of bytes */
)))(i)?
或类似的东西。内部解析器不必是 tuple,但是它需要是消耗8位倍数的位级解析器,否则某些位将在退出 bits上下文时被丢弃。

关于rust - 在标称5.1.2中使用位解析器时找不到正确的类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63570795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com