ruby / fiddle

A libffi wrapper for Ruby.
BSD 2-Clause "Simplified" License
158 stars 36 forks source link

allow IO write to write memoryview object without copying or add IO#write_memoryview #74

Closed dsisnero closed 3 years ago

dsisnero commented 3 years ago

as seen on rubybugs #17832 -

IO#write() - arguments that are not a string will be converted to string using to_s want IO#write to be able to write memoryview objects without conversion to string

require` 'fiddle'
include Fiddle
ptr = Fiddle::Pointer['this is a string']
mv = MemoryView.new ptr
mv.byte_size  #17802 
mv[0]  # 116
'this is a string'.bytes[0] = 116
File.open('test.txt', 'wb'){ f.write mv}
contents = File.open('test.txt', 'r'){ |f| f.read}  # contents is  "#<Fiddle::MemoryView:0x000001a75ae76258>" not 'this is a string'

buffer = ByteBuffer.new('this is a string')
File.open('test.txt', 'w'){|f| f.write Fiddle::MemoryView.new(buffer)}

allow IO#write to write memoryview objects without converting to string or add a new method on IO to write memoryview objects without converting to string

kou commented 3 years ago

Thanks for your report. We can do this now. Fiddle::MemoryView#to_s returns a String but it doesn't copy underlying data.