#bit #serialization #field #byte #packed #pack-unpack #alignment

无std 打包

任意对齐字段的位打包和解包

2个不稳定版本

0.2.0 2020年6月30日
0.1.0 2020年4月21日

#2044编码

Download history 24/week @ 2024-03-17 35/week @ 2024-03-24 93/week @ 2024-03-31 20/week @ 2024-04-07 63/week @ 2024-04-14 38/week @ 2024-04-21 11/week @ 2024-04-28 12/week @ 2024-05-05 22/week @ 2024-05-12 100/week @ 2024-05-19 64/week @ 2024-05-26 17/week @ 2024-06-02 20/week @ 2024-06-09 50/week @ 2024-06-16 107/week @ 2024-06-23 5/week @ 2024-06-30

183 每月下载量
6 个crate(5 个直接使用) 中使用

MIT/Apache

40KB
694

打包

Crate Documentation

此crate提供了一个Packed派生过程宏,它生成将Rust结构体打包和解包为字节序列所需的代码。

有其他几个crate提供了类似的功能,但此crate提供的功能之一是任意对齐字段——字段不需要从字节边界开始或结束。

此外,我希望支持与SCSI和USB规范文档描述其数据包的方式相似的属性。字段属性支持详述形式,其中所有命名属性都是可选的,可以按任何顺序出现

#[打包(start_bit=7,end_bit=5)]

或者一个简洁的形式,需要正好4个值(这种形式与上述规范非常匹配)

#[pkd(7, 5, 0, 0)]

过程宏还生成了一个很棒的Markdown表格,显示了字段对齐情况,可以很容易地与原始规范进行比较。

示例

#[derive(Packed)]
pub enum PeripheralQualifier {
    /// A peripheral device having the specified peripheral device type is connected to this logical unit. If the device server is unable to determine whether or not a peripheral device is connected, it also shall use this peripheral qualifier. This peripheral qualifier does not mean that the peripheral device connected to the logical unit is ready for access.
    Connected = 0b000,
    /// A peripheral device having the specified peripheral device type is not connected to this logical unit. However, the device server is capable of supporting the specified peripheral device type on this logical unit.
    NotConnected = 0b001,
    /// The device server is not capable of supporting a peripheral device on this logical unit. For this peripheral qualifier the peripheral device type shall be set to 1Fh. All other peripheral device type values are reserved for this peripheral qualifier.
    Incapable = 0b011,
}
#[packed(big_endian, lsb0)]
pub struct InquiryResponse {
    #[pkd(7, 5, 0, 0)]
    peripheral_qualifier: PeripheralQualifier,

    #[pkd(4, 0, 0, 0)]
    peripheral_device_type: PeripheralDeviceType,

    ///A removable medium ( RMB ) bit set to zero indicates that the medium is not removable. A RMB bit set to one indicates that the medium is removable.
    #[pkd(7, 7, 1, 1)]
    removable_medium: bool,

    ///The VERSION field indicates the implemented version of this standard and is defined in table 142
    #[pkd(7, 0, 2, 2)]
    version: SpcVersion,

    ///The Normal ACA Supported (NORMACA) bit set to one indicates that the device server supports a NACA bit set to one in the CDB CONTROL byte and supports the ACA task attribute (see SAM-4). A N ORM ACA bit set to zero indicates that the device server does not support a NACA bit set to one and does not support the ACA task attribute.
    #[pkd(5, 5, 3, 3)]
    normal_aca: bool,

    ///The RESPONSE DATA FORMAT field indicates the format of the standard INQUIRY data and shall be set as shown in table 139. A RESPONSE DATA FORMAT field set to 2h indicates that the standard INQUIRY data is in the format defined in this standard. Response data format values less than 2h are obsolete. Response data format values greater than 2h are reserved.
    #[pkd(3, 0, 3, 3)]
    response_data_format: ResponseDataFormat,

    ... additional fields omitted ...
}

InquiryResponse的rustdoc文档包括以下表格

字节 7 6 5 4 3 2 1 0
0 peripheral_qualifier MSB - peripheral_qualifier LSB peripheral_device_type MSB - - - peripheral_device_type LSB
1 removable_medium
2 version MSB - - - - - - version LSB
3 normal_aca hierarchical_support response_data_format MSB - - response_data_format LSB

许可证

自由和开源软件,根据MIT许可证和Apache许可证2.0条款分发。

依赖项

~1.5MB
~40K SLoC