Как установить элемент битового поля, созданного с помощью корзины битового поля? - PullRequest
0 голосов
/ 17 января 2020

Я пытаюсь повторить следующие C битовые поля и функциональность в Rust:

typedef struct
{
    uint8_t directive_code : 4;
    uint8_t directive_subtype_code: 4;
    uint8_t condition_code: 4;
    uint8_t delivery_code: 2;
    uint8_t transaction_status: 2;
} Ack;

Ack ack;
ack.directive_code = 5;
ack.directive_subtype_code = 0;
ack.condition_code = 1;
ack.transaction_status = 2;

Я создал структуру, используя битовое поле ящик, но я не могу ' t выяснить, как установить указанные c именованные элементы. Похоже, что ни один из примеров ящика не покрывает это

use bitfield::bitfield; // 0.13.2

bitfield! {
    pub struct Ack(u16);
    directive_code, _: 3, 0;
    directive_subtype_code, _ : 4, 7;
    condition_code, _: 8, 11;
    delivery_code, _: 12, 13;
    transaction_status, _: 14, 15;
}

1 Ответ

1 голос
/ 17 января 2020

Пример , данный библиотекой , показывает, как указать метод установки (set_field1):

bitfield!{
  pub struct BitField1(u16);
  impl Debug;
  // The fields default to u16
  field1, set_field1: 10, 0;
  pub field2, _ : 12, 3;
}

Вам необходимо предоставить второй аргумент для имени метода установки :

use bitfield::bitfield; // 0.13.2

bitfield! {
    pub struct Ack(u16);
    directive_code, set_directive_code: 3, 0;
    directive_subtype_code, _ : 4, 7;
    condition_code, _: 8, 11;
    delivery_code, _: 12, 13;
    transaction_status, _: 14, 15;
}

fn main() {
    let mut a = Ack(0);
    a.set_directive_code(5);
    println!("{:016b}", a.0)
}

Производит вывод:

0000000000000101
...