Module: Borsh::Writable
Instance Method Summary collapse
- #write_array(x) ⇒ Object
- #write_bool(x) ⇒ Object
- #write_enum(x) ⇒ Object
- #write_f32(f) ⇒ Object
- #write_f64(f) ⇒ Object
- #write_i128(n) ⇒ Object
- #write_i16(n) ⇒ Object
- #write_i32(n) ⇒ Object
- #write_i64(n) ⇒ Object
- #write_i8(n) ⇒ Object
- #write_map(x) ⇒ Object (also: #write_hash)
- #write_object(x) ⇒ Object
- #write_option(x) ⇒ Object
- #write_result(x) ⇒ Object
- #write_set(x) ⇒ Object
- #write_string(x) ⇒ Object
- #write_struct(x) ⇒ Object
- #write_u128(n) ⇒ Object
- #write_u16(n) ⇒ Object
- #write_u32(n) ⇒ Object
- #write_u64(n) ⇒ Object
- #write_u8(n) ⇒ Object
- #write_unit ⇒ Object (also: #write_nil)
- #write_vector(x) ⇒ Object (also: #write_vec)
Instance Method Details
#write_array(x) ⇒ Object
94 95 96 |
# File 'lib/borsh/writable.rb', line 94 def write_array(x) x.each { |e| self.write_object(e) } end |
#write_bool(x) ⇒ Object
28 29 30 |
# File 'lib/borsh/writable.rb', line 28 def write_bool(x) self.write_u8(x ? 1 : 0) end |
#write_enum(x) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/borsh/writable.rb', line 155 def write_enum(x) # An enum should be represented as `[ordinal, value]`: unless x.is_a?(Array) && x.size == 2 && x[0].is_a?(Integer) raise "enum must be [ordinal, value]" end ordinal, value = x self.write_u8(ordinal) self.write_object(value) end |
#write_f32(f) ⇒ Object
81 82 83 |
# File 'lib/borsh/writable.rb', line 81 def write_f32(f) self.write([f].pack('e')) end |
#write_f64(f) ⇒ Object
85 86 87 |
# File 'lib/borsh/writable.rb', line 85 def write_f64(f) self.write([f].pack('E')) end |
#write_i128(n) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/borsh/writable.rb', line 74 def write_i128(n) # Convert negative numbers to two's complement: n = (1 << 128) + n if n < 0 # Use `#write_u128` to write the bits: self.write_u128(n) end |
#write_i16(n) ⇒ Object
62 63 64 |
# File 'lib/borsh/writable.rb', line 62 def write_i16(n) self.write([n].pack('s')) end |
#write_i32(n) ⇒ Object
66 67 68 |
# File 'lib/borsh/writable.rb', line 66 def write_i32(n) self.write([n].pack('l<')) end |
#write_i64(n) ⇒ Object
70 71 72 |
# File 'lib/borsh/writable.rb', line 70 def write_i64(n) self.write([n].pack('q<')) end |
#write_i8(n) ⇒ Object
58 59 60 |
# File 'lib/borsh/writable.rb', line 58 def write_i8(n) self.write([n].pack('c')) end |
#write_map(x) ⇒ Object Also known as: write_hash
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/borsh/writable.rb', line 117 def write_map(x) self.write_u32(x.size) case x when Array x.sort.each do |(k, v)| self.write_object(k) self.write_object(v) end when Hash x.keys.sort.each do |k| self.write_object(k) self.write_object(x[k]) end else raise "unsupported type: #{x.class}" end end |
#write_object(x) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/borsh/writable.rb', line 6 def write_object(x) x = x.to_borsh if x.respond_to?(:to_borsh) case x when NilClass then self.write_unit() when FalseClass then self.write_bool(x) when TrueClass then self.write_bool(x) when Integer then self.write_i64(x) when Float then self.write_f64(x) when Symbol then self.write_string(x.to_s) when String then self.write_string(x) when Array then self.write_vector(x) when Hash then self.write_map(x) when Set then self.write_set(x) else raise "unsupported type: #{x.class}" end end |
#write_option(x) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/borsh/writable.rb', line 135 def write_option(x) if !x.nil? self.write_u8(1) self.write_object(x) else self.write_u8(0) end end |
#write_result(x) ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/borsh/writable.rb', line 144 def write_result(x) # A result should be represented as `[ok, value]`: unless x.is_a?(Array) && x.size == 2 raise "result must be [ok, value]" end ok, value = x self.write_u8(ok ? 1 : 0) self.write_object(value) end |
#write_set(x) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/borsh/writable.rb', line 104 def write_set(x) self.write_u32(x.size) keys = case x when Array then x when Hash then x.keys when Set then x.to_a else raise "unsupported type: #{x.class}" end keys.sort.each do |k| self.write_object(k) end end |
#write_string(x) ⇒ Object
89 90 91 92 |
# File 'lib/borsh/writable.rb', line 89 def write_string(x) self.write_u32(x.bytesize) self.write(x) end |
#write_struct(x) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/borsh/writable.rb', line 166 def write_struct(x) raise "value must be a Struct" unless x.is_a?(Struct) x.members.each do |k| self.write_object(x[k]) end end |
#write_u128(n) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/borsh/writable.rb', line 48 def write_u128(n) # Split 128-bit number into two 64-bit parts: lower = n & ((1 << 64) - 1) upper = (n >> 64) & ((1 << 64) - 1) # Write the lower 64 bits first (little-endian): self.write_u64(lower) # Then write the upper 64 bits: self.write_u64(upper) end |
#write_u16(n) ⇒ Object
36 37 38 |
# File 'lib/borsh/writable.rb', line 36 def write_u16(n) self.write([n].pack('v')) end |
#write_u32(n) ⇒ Object
40 41 42 |
# File 'lib/borsh/writable.rb', line 40 def write_u32(n) self.write([n].pack('V')) end |
#write_u64(n) ⇒ Object
44 45 46 |
# File 'lib/borsh/writable.rb', line 44 def write_u64(n) self.write([n].pack('Q<')) end |
#write_u8(n) ⇒ Object
32 33 34 |
# File 'lib/borsh/writable.rb', line 32 def write_u8(n) self.write([n].pack('C')) end |
#write_unit ⇒ Object Also known as: write_nil
23 24 25 |
# File 'lib/borsh/writable.rb', line 23 def write_unit() # nothing to write end |
#write_vector(x) ⇒ Object Also known as: write_vec
98 99 100 101 |
# File 'lib/borsh/writable.rb', line 98 def write_vector(x) self.write_u32(x.size) x.each { |e| self.write_object(e) } end |