module Spotify::UTF8StringPointer

The NULString is used to allow you to assign struct fields with regular ruby strings. Usually, it would raise an error.

Keep in mind this implementation is unsafe to use on Rubinius as long as it ignores the .reference_required? indication.

Public Class Methods

from_native(value, ctx) click to toggle source

Given a pointer, read out it’s string.

@param [FFI::Pointer] value @param ctx @return [String, nil]

# File lib/spotify/data_converters/utf8_string_pointer.rb, line 27
def from_native(value, ctx)
  value.read_string.force_encoding(Encoding::UTF_8) unless value.null?
end
reference_required?() click to toggle source

Used by FFI::StructLayoutField to know if this field requires the reference to be maintained by FFI. If we return false here, the MemoryPointer from to_native will be garbage collected before the struct.

# File lib/spotify/data_converters/utf8_string_pointer.rb, line 35
def reference_required?
  true
end
to_native(value, ctx) click to toggle source

Given either a String or nil, make an actual FFI::Pointer of that value.

@param [#to_str, nil] value @param ctx @return [FFI::Pointer]

# File lib/spotify/data_converters/utf8_string_pointer.rb, line 18
def to_native(value, ctx)
  value && FFI::MemoryPointer.from_string(value.to_str.encode(Encoding::UTF_8))
end