Debian Codegraph @

src/generation/bit_operations.rs

raw symbols SCIP
1 use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#;
2 use quoterust-analyzer cargo quote 1.0.40 crate/::quoterust-analyzer cargo quote 1.0.40 quote!;
3 use synrust-analyzer cargo syn 2.0.104 crate/::Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#;
4
5 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::commonrust-analyzer cargo bitfields-impl 1.0.0 generation/common/::{does_field_have_getterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_getter()., does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter().};
6 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::bitfield_fieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/::BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#;
7 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::typesrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/::get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().;
8
9 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_get_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_get_bit_tokens().(
10 vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#,
11 bitfield_type: &synrust-analyzer cargo syn 2.0.104 crate/::Typerust-analyzer cargo syn 2.0.104 ty/Type#,
12 fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
13 ignored_fields_struct: bool,
14 ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
15 let bitfield_type_bits = get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(bitfield_type).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() as usize;
16 let get_bit_documentation = "Returns a bit from the given index. Returns false for out-of-bounds and fields without read access.".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
17 let checked_get_bit_documentation = "Returns a bit from the given index. Returns an error for out-of-bounds and fields without read access.".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
18
19 let false_return_for_non_readable_fields = fields
20 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
21 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().does_field_have_getterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_getter().(field) && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().field.paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.)
22 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| {
23 let field_bits = field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits. as usize;
24 let field_offset = field.offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset. as usize;
25 let field_end_bits = field_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().field_bits;
26 quoterust-analyzer cargo quote 1.0.40 quote!! {
27 if index >= #field_offset && index < #field_end_bits {
28 return false;
29 }
30 }
31 });
32
33 let error_return_for_write_only_fields = fields
34 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
35 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().does_field_have_getterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_getter().(field) && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().field.paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.)
36 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| {
37 let field_bits = field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits. as usize;
38 let field_offset = field.offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset. as usize;
39 let field_end_bits = field_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().field_bits;
40 quoterust-analyzer cargo quote 1.0.40 quote!! {
41 if index >= #field_offset && index < #field_end_bits {
42 return Err("Can't read from a write-only field.");
43 }
44 }
45 });
46
47 let struct_val_ident = if ignored_fields_struct {
48 quoterust-analyzer cargo quote 1.0.40 quote!! { self.val }
49 } else {
50 quoterust-analyzer cargo quote 1.0.40 quote!! { self.0 }
51 };
52
53 quoterust-analyzer cargo quote 1.0.40 quote!! {
54 #[doc = #get_bit_documentation]
55 #vis const fn get_bit(&self, index: usize) -> bool {
56 if index > #bitfield_type_bits {
57 return false;
58 }
59
60 #( #false_return_for_non_readable_fields )*
61
62 (#struct_val_ident >> index) & 1 != 0
63 }
64
65 #[doc = #checked_get_bit_documentation]
66 #vis const fn checked_get_bit(&self, index: usize) -> Result<bool, &'static str> {
67 if index > #bitfield_type_bits {
68 return Err("Index out of bounds.");
69 }
70
71 #( #error_return_for_write_only_fields )*
72
73 Ok((#struct_val_ident >> index) & 1 != 0)
74 }
75 }
76 }
77
78 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_set_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_set_bit_tokens().(
79 vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#,
80 bitfield_type: &synrust-analyzer cargo syn 2.0.104 crate/::Typerust-analyzer cargo syn 2.0.104 ty/Type#,
81 fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
82 ignored_fields_struct: bool,
83 ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
84 let bitfield_type_bits = get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(bitfield_type).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() as usize;
85 let set_bit_documentation = "Sets a bit at given index with the given value. Is no-op for out-of-bounds and fields without write access.".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
86 let checked_set_bit_documentation = "Sets a bit at given index with the given value. Returns an error for out-of-bounds and fields without write access.".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
87
88 let no_op_for_non_writable_fields =
89 fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter().(field)).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| {
90 let field_bits = field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits. as usize;
91 let field_offset = field.offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset. as usize;
92 let field_end_bits = field_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().field_bits;
93 quoterust-analyzer cargo quote 1.0.40 quote!! {
94 if index >= #field_offset && index < #field_end_bits {
95 return;
96 }
97 }
98 });
99
100 let error_return_for_non_writable_fields =
101 fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter().(field)).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| {
102 let field_bits = field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits. as usize;
103 let field_offset = field.offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset. as usize;
104 let field_end_bits = field_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().field_bits;
105 quoterust-analyzer cargo quote 1.0.40 quote!! {
106 if index >= #field_offset && index < #field_end_bits {
107 return Err("Can't write to a non-writable or padding field.");
108 }
109 }
110 });
111
112 let struct_val_ident = if ignored_fields_struct {
113 quoterust-analyzer cargo quote 1.0.40 quote!! { self.val }
114 } else {
115 quoterust-analyzer cargo quote 1.0.40 quote!! { self.0 }
116 };
117
118 quoterust-analyzer cargo quote 1.0.40 quote!! {
119 #[doc = #set_bit_documentation]
120 #vis const fn set_bit(&mut self, index: usize, bit: bool) {
121 if index > #bitfield_type_bits {
122 return;
123 }
124
125 #( #no_op_for_non_writable_fields )*
126
127 if bit {
128 #struct_val_ident |= 1 << index;
129 } else {
130 #struct_val_ident &= !(1 << index);
131 }
132 }
133
134 #[doc = #checked_set_bit_documentation]
135 #vis const fn checked_set_bit(&mut self, index: usize, bit: bool) -> Result<(), &'static str> {
136 if index > #bitfield_type_bits {
137 return Err("Index out of bounds.");
138 }
139
140 #( #error_return_for_non_writable_fields )*
141
142 if bit {
143 #struct_val_ident |= 1 << index;
144 } else {
145 #struct_val_ident &= !(1 << index);
146 }
147
148 Ok(())
149 }
150 }
151 }