【问题标题】:What am I supposed to pass to the following method in Crystal?我应该将什么传递给 Crystal 中的以下方法?
【发布时间】:2021-02-12 10:41:49
【问题描述】:

背景

我正在尝试使用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

【问题讨论】:

    标签: methods syntax semantics crystal-lang


    【解决方案1】:

    我终于让它工作了。或者好吧,我至少设法调用它。

    surface.write_to_png_stream ->my_write_func(Void*, UInt8*, UInt32), Pointer(Void).null
    

    事实证明它毕竟是一个 Proc,正如我所怀疑的那样,我也只需要一个空指针。 为了将来参考,我有一个issue at the repo 谈论这个方法的具体用法,但我想语法/语义问题在这里回答了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多