Method: FFI::Bitmask#from_native

Defined in:
lib/ffi/enum.rb

#from_native(val, ctx) ⇒ Array<Symbol, Integer>

Returns list of symbol names corresponding to val, plus an optional remainder if some bits don’t match any constant.

Parameters:

  • val (Integer)
  • ctx

    unused

Returns:

  • (Array<Symbol, Integer>)

    list of symbol names corresponding to val, plus an optional remainder if some bits don’t match any constant



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ffi/enum.rb', line 288

def from_native(val, ctx)
  flags = @kv_map.select { |_, v| v & val != 0 }
  list = flags.keys
  # force an unsigned value of the correct size
  val &= (1 << (@native_type.size * 8)) - 1 if @signed
  # If there are unmatch flags,
  # return them in an integer,
  # else information can be lost.
  # Similar to Enum behavior.
  remainder = val ^ flags.values.reduce(0, :|)
  list.push remainder unless remainder == 0
  return list
end