bytebuffer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ class ByteBuffer
2
+ # @!attribute [r] VERSION
3
+ # @return [Integer] version number of the library
4
+ VERSION = '0.1.0'
5
+ end
data/lib/bytebuffer.rb ADDED
@@ -0,0 +1,218 @@
1
+ require 'ffi'
2
+ require_relative 'bytebuffer/version'
3
+
4
+ # This module extends the FFI::Library to provide functions used to interact with the compiled rust library.
5
+ module ByteBufferExtension
6
+ extend FFI::Library
7
+
8
+ ffi_lib("ext/libext.#{FFI::Platform::LIBSUFFIX}")
9
+
10
+ attach_function(:new, :new, [], :pointer)
11
+ attach_function(:from_bytes, :from_bytes, [:pointer, :int], :pointer)
12
+
13
+ attach_function(:drop, :drop, [:pointer], :void)
14
+
15
+ attach_function(:length, :length, [:pointer], :int)
16
+
17
+ attach_function(:set_write_position, :set_write_position, [:pointer, :int], :void)
18
+ attach_function(:set_read_position, :set_read_position, [:pointer, :int], :void)
19
+ attach_function(:get_write_position, :get_write_position, [:pointer], :int)
20
+ attach_function(:get_read_position, :get_read_position, [:pointer], :int)
21
+
22
+ attach_function(:reset_cursors, :reset_cursors, [:pointer], :void)
23
+ attach_function(:reset_bits_cursor, :reset_bits_cursor, [:pointer], :void)
24
+ attach_function(:resize, :resize, [:pointer, :int], :void)
25
+
26
+ attach_function(:clear, :clear, [:pointer], :void)
27
+ attach_function(:is_empty?, :is_empty, [:pointer], :bool)
28
+
29
+ attach_function(:read_bit, :read_bit, [:pointer], :bool)
30
+ attach_function(:read_bits, :read_bits, [:pointer, :int], :long)
31
+ attach_function(:read_u8, :read_u8, [:pointer], :int)
32
+ attach_function(:read_i8, :read_i8, [:pointer], :int)
33
+ attach_function(:read_u16, :read_u16, [:pointer], :int)
34
+ attach_function(:read_i16, :read_i16, [:pointer], :int)
35
+ attach_function(:read_u32, :read_u32, [:pointer], :int)
36
+ attach_function(:read_i32, :read_i32, [:pointer], :int)
37
+ attach_function(:read_u64, :read_u64, [:pointer], :long)
38
+ attach_function(:read_i64, :read_i64, [:pointer], :long)
39
+ attach_function(:read_f32, :read_f32, [:pointer], :float)
40
+ attach_function(:read_f64, :read_f64, [:pointer], :double)
41
+
42
+ attach_function(:write_bit, :write_bit, [:pointer, :bool], :void)
43
+ attach_function(:write_bits, :write_bits, [:pointer, :int, :int], :void)
44
+ attach_function(:write_u8, :write_u8, [:pointer, :int], :void)
45
+ attach_function(:write_i8, :write_i8, [:pointer, :int], :void)
46
+ attach_function(:write_u16, :write_u16, [:pointer, :int], :void)
47
+ attach_function(:write_i16, :write_i16, [:pointer, :int], :void)
48
+ attach_function(:write_u32, :write_u32, [:pointer, :int], :void)
49
+ attach_function(:write_i32, :write_i32, [:pointer, :int], :void)
50
+ attach_function(:write_u64, :write_u64, [:pointer, :long], :void)
51
+ attach_function(:write_i64, :write_i64, [:pointer, :long], :void)
52
+ attach_function(:write_f32, :write_f32, [:pointer, :float], :void)
53
+ attach_function(:write_f64, :write_f64, [:pointer, :double], :void)
54
+ end
55
+
56
+ # A {ByteBuffer} provides functions for interacting with buffered IO data. This gem wraps the {bytebuffer} rust crate located at https://github.com/terahlunah/bytebuffer using FFI.
57
+ class ByteBuffer
58
+
59
+
60
+
61
+ # Constructs a new ByteBuffer.
62
+ def initialize; @ptr = ByteBufferExtension.new; end
63
+
64
+ # Returns the length of the ByteBuffer.
65
+ # @return [Integer] the length
66
+ def length; ByteBufferExtension.length(@ptr); end
67
+
68
+ # Resets the read and write cursor positions.
69
+ def reset_cursors; ByteBufferExtension.reset_cursors(@ptr); end
70
+
71
+ # Resets the bit cursor position.
72
+ def reset_bits_cursor; ByteBufferExtension.reset_bits_cursor(@ptr); end
73
+
74
+ # Clear all bytes from the {ByteBuffer}.
75
+ def clear; ByteBufferExtension.clear(@ptr); end
76
+
77
+ # Is the {ByteBuffer} empty?
78
+ # @return [Boolean] true if the {ByteBuffer} is empty, false otherwise.
79
+ def is_empty?; ByteBufferExtension.is_empty?(@ptr); end
80
+
81
+ # Resizes the {ByteBuffer} to the desired length.
82
+ # @note {ByteBuffer}s can only increase in size. Lower or negative sizes will not work.
83
+ # @param new_size [Integer] the desired length.
84
+ def resize(new_size)
85
+ if new_size.negative?
86
+ raise ArgumentError, "new_size must be positive"
87
+ else
88
+ ByteBufferExtension.resize(@ptr, new_size)
89
+ end
90
+ end
91
+
92
+ # Frees the memory allocated for this {ByteBuffer}.
93
+ # @note This function will invoke the drop() macro in rust, passing in the pointer for the {ByteBuffer}. After the call is made, the pointer is set to `nil`.
94
+ def free
95
+ ByteBufferExtension.drop(@ptr)
96
+ @ptr = nil # :gottem:
97
+ end
98
+
99
+ # Set the write cursor to the desired position.
100
+ # @param position [Integer] the desired position.
101
+ def set_write_position(position); ByteBufferExtension.set_write_position(@ptr, position); end
102
+
103
+ # Set the read cursor to the desired position.
104
+ # @param position [Integer] the desired position.
105
+ def set_read_position(position); ByteBufferExtension.set_read_position(@ptr, position); end
106
+
107
+ # Gets the write cursor's current position.
108
+ # @return [Integer] the write cursor's position.
109
+ def get_write_position; ByteBufferExtension.get_write_position(@ptr); end
110
+
111
+ # Gets the read cursor's current position.
112
+ # @return [Integer] the read cursor's position.
113
+ def get_read_position; ByteBufferExtension.get_read_position(@ptr); end
114
+
115
+ # Write a boolean value as a single bit to the {ByteBuffer}.
116
+ # @param value [Boolean] the value to write.
117
+ def write_bit(value); ByteBufferExtension.write_bit(@ptr, value); end
118
+
119
+ # Write a numeric value as a series of bits in the {ByteBuffer}.
120
+ # @param value [Integer] the value to write.
121
+ # @param amount [Integer] the amount of bits to use.
122
+ def write_bits(value, amount); ByteBufferExtension.write_bits(@ptr, value, amount); end
123
+
124
+ # Write a numeric value as a 8-bit unsigned integer in the {ByteBuffer}.
125
+ # @param value [Integer] the value to write.
126
+ def write_u8(value); ByteBufferExtension.write_u8(@ptr, value); end
127
+
128
+ # Write a numeric value as a 8-bit signed integer in the {ByteBuffer}.
129
+ # @param value [Integer] the value to write.
130
+ def write_i8(value); ByteBufferExtension.write_i8(@ptr, value); end
131
+
132
+ # Write a numeric value as a 16-bit unsigned integer in the {ByteBuffer}.
133
+ # @param value [Integer] the value to write.
134
+ def write_u16(value); ByteBufferExtension.write_u16(@ptr, value); end
135
+
136
+ # Write a numeric value as a 16-bit signed integer in the {ByteBuffer}.
137
+ # @param value [Integer] the value to write.
138
+ def write_i16(value); ByteBufferExtension.write_i16(@ptr, value); end
139
+
140
+ # Write a numeric value as a 32-bit unsigned integer in the {ByteBuffer}.
141
+ # @param value [Integer] the value to write.
142
+ def write_u32(value); ByteBufferExtension.write_u32(@ptr, value); end
143
+
144
+ # Write a numeric value as a 32-bit signed integer in the {ByteBuffer}.
145
+ # @param value [Integer] the value to write.
146
+ def write_i32(value); ByteBufferExtension.write_i32(@ptr, value); end
147
+
148
+ # Write a numeric value as a 64-bit unsigned integer in the {ByteBuffer}.
149
+ # @param value [Integer] the value to write.
150
+ def write_u64(value); ByteBufferExtension.write_u64(@ptr, value); end
151
+
152
+ # Write a numeric value as a 64-bit signed integer in the {ByteBuffer}.
153
+ # @param value [Integer] the value to write.
154
+ def write_i64(value); ByteBufferExtension.write_i64(@ptr, value); end
155
+
156
+ # Write a float value as a 32-bit signed float in the {ByteBuffer}.
157
+ # @note The C value used is accurate to the nearest millionth decimal place.
158
+ # @param value [Float] the value to write.
159
+ def write_f32(value); ByteBufferExtension.write_f32(@ptr, value); end
160
+
161
+ # Write a float value as a 64-bit signed float in the {ByteBuffer}.
162
+ # @note The C value used is accurate to the nearest millionth decimal place.
163
+ # @param value [Float] the value to write.
164
+ def write_f64(value); ByteBufferExtension.write_f64(@ptr, value); end
165
+
166
+ # Read a single bit from the {ByteBuffer} as a boolean value.
167
+ # @return [Boolean] the read value.
168
+ def read_bit; ByteBufferExtension.read_bit(@ptr); end
169
+
170
+ # Read a series of bits from the {ByteBuffer} as a single unsigned 64-bit integer.
171
+ # @return [Integer] the read value.
172
+ def read_bits(amount); ByteBufferExtension.read_bits(@ptr, amount); end
173
+
174
+ # Read a single 8-bit unsigned integer from the {ByteBuffer}.
175
+ # @return [Integer] the read value.
176
+ def read_u8; ByteBufferExtension.read_u8(@ptr); end
177
+
178
+ # Read a single 8-bit signed integer from the {ByteBuffer}.
179
+ # @return [Integer] the read value.
180
+ def read_i8; ByteBufferExtension.read_i8(@ptr); end
181
+
182
+ # Read a single 16-bit unsigned integer from the {ByteBuffer}.
183
+ # @return [Integer] the read value.
184
+ def read_u16; ByteBufferExtension.read_u16(@ptr); end
185
+
186
+ # Read a single 16-bit signed integer from the {ByteBuffer}.
187
+ # @return [Integer] the read value.
188
+ def read_i16; ByteBufferExtension.read_i16(@ptr); end
189
+
190
+ # Read a single 32-bit unsigned integer from the {ByteBuffer}.
191
+ # @return [Integer] the read value.
192
+ def read_u32; ByteBufferExtension.read_u32(@ptr); end
193
+
194
+ # Read a single 32-bit signed integer from the {ByteBuffer}.
195
+ # @return [Integer] the read value.
196
+ def read_i32; ByteBufferExtension.read_i32(@ptr); end
197
+
198
+ # Read a single 64-bit unsigned integer from the {ByteBuffer}.
199
+ # @return [Integer] the read value.
200
+ def read_u64; ByteBufferExtension.read_u64(@ptr); end
201
+
202
+ # Read a single 64-bit signed integer from the {ByteBuffer}.
203
+ # @return [Integer] the read value.
204
+ def read_i64; ByteBufferExtension.read_i64(@ptr); end
205
+
206
+ # Read a single 32-bit signed float from the {ByteBuffer}.
207
+ # @note Float values read from this buffer are accurate to the millionth decimal place.
208
+ # @return [Float] the read value.
209
+ def read_f32; ByteBufferExtension.read_f32(@ptr); end
210
+
211
+ # Read a single 32-bit signed float from the {ByteBuffer}.
212
+ # @note Float values read from this buffer are accurate to the millionth decimal place.
213
+ # @return [Float] the read value.
214
+ def read_f64; ByteBufferExtension.read_f64(@ptr); end
215
+
216
+ alias_method :drop, :free
217
+ alias_method :destroy, :free
218
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bytebuffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick W.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ description: A gem that provides a ByteBuffer class based on the bytebuffer rust crate.
56
+ email: Sickday@pm.me
57
+ executables: []
58
+ extensions:
59
+ - ext/extconf.rb
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/pages.yml"
63
+ - ".github/workflows/publish.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bytebuffer.gemspec
70
+ - ext/Cargo.lock
71
+ - ext/Cargo.toml
72
+ - ext/Makefile
73
+ - ext/extconf.rb
74
+ - ext/src/lib.rs
75
+ - lib/bytebuffer.rb
76
+ - lib/bytebuffer/version.rb
77
+ homepage:
78
+ licenses:
79
+ - BSD-3-Clause
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.1.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.5.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A gem wrapping the bytebuffer crate using FFI.
100
+ test_files: []