我应该将什么传递给 Crystal 中的以下方法?
What am I supposed to pass to the following method in Crystal?
背景
我正在尝试使用 some Cairo bindings for Crystal,但我在语法以及如何调用下面的方法方面遇到了一些问题。
它是这样实现的:
# Inside Module Cairo, class Surface
# ...
def write_to_png_stream(write_func : LibCairo::WriteFuncT, closure : Void*) : Status
Status.new(LibCairo.surface_write_to_png_stream(@surface, write_func, closure).value)
end
# Inside Module LibCairo
# ...
enum StatusT
SUCCESS = 0
NO_MEMORY
INVALID_RESTORE
# ...
end
# ...
alias WriteFuncT = Void*, UInt8*, UInt32 -> StatusT
# ...
fun surface_write_to_png_stream = cairo_surface_write_to_png_stream(
surface : PSurfaceT,
write_func : WriteFuncT,
closure : Void*
) : StatusT
问题
具体来说,我问的是如何调用 Cairo::Surface#write_to_png_stream 方法。我传递什么作为 write_func:LibCairo::WriteFuncT
?我传递什么作为 closure: Void*
?
我尝试了以下方法,但未能成功...
def my_write_func(a : Void*, b : UInt8*, c : UInt32) : Cairo::C::LibCairo::StatusT
puts a
puts b
puts c
Cairo::C::LibCairo::StatusT::SUCCESS
end
surface = Cairo::Surface.new Cairo::Format::ARGB32, 400, 300
ctx = Cairo::Context.new surface
ctx.set_source_rgba 1.0, 0.0, 1.0, 1.0
ctx.rectangle 0.0, 0.0, 400.0, 300.0
ctx.fill
# here, how do I call surface.write_to_png_stream passing my 'my_write_func'?
# a Proc doesn't seem to work.. ( ->my_write_func(Void*, UInt8*, UInt32) )
surface.finish
终于我让它工作了..或者至少我设法调用了它。
surface.write_to_png_stream ->my_write_func(Void*, UInt8*, UInt32), Pointer(Void).null
事实证明,正如我所怀疑的那样,它毕竟是一个 Proc,而且我也只需要一个空指针。
为了将来参考,我有一个 issue at the repo 谈论这个方法的具体用法,但我想 syntax/semantics 问题在这里得到了回答。
背景
我正在尝试使用 some Cairo bindings for Crystal,但我在语法以及如何调用下面的方法方面遇到了一些问题。 它是这样实现的:
# Inside Module Cairo, class Surface
# ...
def write_to_png_stream(write_func : LibCairo::WriteFuncT, closure : Void*) : Status
Status.new(LibCairo.surface_write_to_png_stream(@surface, write_func, closure).value)
end
# Inside Module LibCairo
# ...
enum StatusT
SUCCESS = 0
NO_MEMORY
INVALID_RESTORE
# ...
end
# ...
alias WriteFuncT = Void*, UInt8*, UInt32 -> StatusT
# ...
fun surface_write_to_png_stream = cairo_surface_write_to_png_stream(
surface : PSurfaceT,
write_func : WriteFuncT,
closure : Void*
) : StatusT
问题
具体来说,我问的是如何调用 Cairo::Surface#write_to_png_stream 方法。我传递什么作为 write_func:LibCairo::WriteFuncT
?我传递什么作为 closure: Void*
?
我尝试了以下方法,但未能成功...
def my_write_func(a : Void*, b : UInt8*, c : UInt32) : Cairo::C::LibCairo::StatusT
puts a
puts b
puts c
Cairo::C::LibCairo::StatusT::SUCCESS
end
surface = Cairo::Surface.new Cairo::Format::ARGB32, 400, 300
ctx = Cairo::Context.new surface
ctx.set_source_rgba 1.0, 0.0, 1.0, 1.0
ctx.rectangle 0.0, 0.0, 400.0, 300.0
ctx.fill
# here, how do I call surface.write_to_png_stream passing my 'my_write_func'?
# a Proc doesn't seem to work.. ( ->my_write_func(Void*, UInt8*, UInt32) )
surface.finish
终于我让它工作了..或者至少我设法调用了它。
surface.write_to_png_stream ->my_write_func(Void*, UInt8*, UInt32), Pointer(Void).null
事实证明,正如我所怀疑的那样,它毕竟是一个 Proc,而且我也只需要一个空指针。 为了将来参考,我有一个 issue at the repo 谈论这个方法的具体用法,但我想 syntax/semantics 问题在这里得到了回答。