ffi-bitfield 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50775cc156bce79d4d20153303484ba80d19d23707d5413ea2557676260e8b38
4
- data.tar.gz: 941e057d7cd548da1fd133f81143e573d7795c060d7678334c74cd1ed7cdaa31
3
+ metadata.gz: da1a5728a9f03382d8de48da89ad38e95ddec9e493288b5f5954fc0eeed34f24
4
+ data.tar.gz: f14ac752955f7a13a0753099cb85b46ff6e7d8bb20f559baea397ad6fa10375d
5
5
  SHA512:
6
- metadata.gz: 7329ad0a6bf66f580af0fdbbc3219774a53e7624a78e8e3891562150444282c0b2aae26b5eb4382edf8f67e27e66224aaf32592757f6e813105ab432a5e646a5
7
- data.tar.gz: 3e4bb498d57114b6f9ccaf140d5789239609ff402f3be888d419fefa66957547659f1d5ab8577439f9fb98ad89fa6d56869f30a985b04486bc9bc90799c9b82a
6
+ metadata.gz: 94d9b4e73951519a5f3af827ee4d3131891cf0586f7c01f1a7e83057fcbd27580e3edf61cc5176190ed9fc86feb73e392b122c5c7ea75ba93333609d9bc5bfe1
7
+ data.tar.gz: 1c652488233f533fb78e2724cb9070e81dd5cf8915e241676f7c60b8e8ecfb4de1c4fb651daf0e2f993d340fbc3a343c9ad4acd9dbebaa08308981f7af9e77c9
data/README.md CHANGED
@@ -70,6 +70,42 @@ s[:y] = 1
70
70
  p s[:a] # 64
71
71
  ```
72
72
 
73
+ ### Inspecting Bit Fields
74
+
75
+ You can use the `bit_field_members` method to get a hash of bit fields grouped by parent field:
76
+
77
+ ```ruby
78
+ class Flags < FFI::BitStruct
79
+ layout \
80
+ :value, :uint8
81
+
82
+ bit_fields :value,
83
+ :read, 1,
84
+ :write, 1,
85
+ :execute, 1,
86
+ :unused, 5
87
+ end
88
+
89
+ p Flags.bit_field_members
90
+ # => {:value=>[:read, :write, :execute, :unused]}
91
+ ```
92
+
93
+ For more detailed information, you can use the `bit_field_layout` method:
94
+
95
+ ```ruby
96
+ p Flags.bit_field_layout
97
+ # => {
98
+ # :value => {
99
+ # :read => { :start => 0, :width => 1 },
100
+ # :write => { :start => 1, :width => 1 },
101
+ # :execute => { :start => 2, :width => 1 },
102
+ # :unused => { :start => 3, :width => 5 }
103
+ # }
104
+ # }
105
+ ```
106
+
107
+ These methods are useful for custom pretty printing or introspection of your bit struct classes.
108
+
73
109
  ### Loading
74
110
 
75
111
  ```ruby
@@ -5,6 +5,72 @@ module FFI
5
5
  # Layout provides methods for defining bit field layouts.
6
6
  # This module is extended by BitStruct and ManagedBitStruct classes.
7
7
  module Layout
8
+ # Returns a hash of bit fields grouped by parent field.
9
+ #
10
+ # @return [Hash] A hash where keys are parent field names and values are arrays of bit field names
11
+ #
12
+ # @example Get bit field members in a struct
13
+ # class Flags < FFI::BitStruct
14
+ # layout \
15
+ # :value, :uint8
16
+ #
17
+ # bit_fields :value,
18
+ # :read, 1,
19
+ # :write, 1,
20
+ # :execute, 1,
21
+ # :unused, 5
22
+ # end
23
+ #
24
+ # Flags.bit_field_members # => {:value => [:read, :write, :execute, :unused]}
25
+ def bit_field_members
26
+ return {} unless instance_variable_defined?(:@bit_field_hash_table)
27
+
28
+ result = {}
29
+ @bit_field_hash_table.each do |field_name, info|
30
+ parent_name = info[0]
31
+ result[parent_name] ||= []
32
+ result[parent_name] << field_name
33
+ end
34
+ result
35
+ end
36
+
37
+ # Returns a hash of bit fields with detailed layout information.
38
+ #
39
+ # @return [Hash] A hash where keys are parent field names and values are hashes of bit field details
40
+ #
41
+ # @example Get detailed bit field layout in a struct
42
+ # class Flags < FFI::BitStruct
43
+ # layout \
44
+ # :value, :uint8
45
+ #
46
+ # bit_fields :value,
47
+ # :read, 1,
48
+ # :write, 1,
49
+ # :execute, 1,
50
+ # :unused, 5
51
+ # end
52
+ #
53
+ # Flags.bit_field_layout
54
+ # # => {
55
+ # # :value => {
56
+ # # :read => { :start => 0, :width => 1 },
57
+ # # :write => { :start => 1, :width => 1 },
58
+ # # :execute => { :start => 2, :width => 1 },
59
+ # # :unused => { :start => 3, :width => 5 }
60
+ # # }
61
+ # # }
62
+ def bit_field_layout
63
+ return {} unless instance_variable_defined?(:@bit_field_hash_table)
64
+
65
+ result = {}
66
+ @bit_field_hash_table.each do |field_name, info|
67
+ parent_name, start, width = info
68
+ result[parent_name] ||= {}
69
+ result[parent_name][field_name] = { start: start, width: width }
70
+ end
71
+ result
72
+ end
73
+
8
74
  # Defines bit fields within a parent field.
9
75
  #
10
76
  # @param [Array] layout_args An array where the first element is the parent field name,
@@ -3,6 +3,6 @@
3
3
  module FFI
4
4
  module BitField
5
5
  # Current version of the ffi-bitfield gem.
6
- VERSION = '0.0.8'
6
+ VERSION = '0.0.9'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-bitfield
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 2025-03-21 00:00:00.000000000 Z
11
+ date: 2025-04-10 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: ffi
@@ -42,6 +43,7 @@ homepage: https://github.com/kojix2/ffi-bitfield
42
43
  licenses:
43
44
  - MIT
44
45
  metadata: {}
46
+ post_install_message:
45
47
  rdoc_options: []
46
48
  require_paths:
47
49
  - lib
@@ -56,7 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
58
  - !ruby/object:Gem::Version
57
59
  version: '0'
58
60
  requirements: []
59
- rubygems_version: 3.6.2
61
+ rubygems_version: 3.5.22
62
+ signing_key:
60
63
  specification_version: 4
61
64
  summary: bit fields for Ruby-FFI
62
65
  test_files: []