我希望能够与REPR CStruct / CPointer类使用双指针:
typedef struct CipherContext {
void *cipher;
const uint8_t *key;
size_t key_len;
const uint8_t *path;
size_t path_len;
size_t block_size;
void *handle;
int (*cipher_init)(void **, const uint8_t *, size_t);
int (*cipher_encode)(void *, const uint8_t *, uint8_t *, size_t);
int (*cipher_decode)(void *, const uint8_t *, uint8_t *, size_t);
void (*cipher_free)(void *);
const uint8_t *(*cipher_strerror)(int);
} CipherContext;
int cipher_context_init(CipherContext **, const uint8_t *, size_t, const uint8_t *, size_t, size_t);
int cipher_context_encode(CipherContext *, const uint8_t *, uint8_t *, size_t);
int cipher_context_decode(CipherContext *, const uint8_t *, uint8_t *, size_t);
void cipher_context_free(CipherContext *);
const uint8_t *cipher_context_strerror(int);
Perl 6的代码:
method new(Blob :$key!, Str :$path!, Int :$block-size!) {
my Pointer[::?CLASS] $ptr .= new;
my Int $err = cipher_context_init($ptr, $key, $key.elems, $path, $path.codes, $block-size);
return $ptr.deref unless $err;
my Str $errstr = cipher_context_strerror($err) || do {
my &cipher-strerror = nativecast(:(int32 --> Str), $!cipher-strerror);
cipher-strerror($err)
};
die "Failed to initialize cipher context: $errstr";
}
submethod DESTROY() {
cipher_context_free(self)
}
短高尔夫:
use v6.d;
use Nativecall;
class Foo is repr('CPointer') {
my Pointer[::?CLASS] $foo .= nw;
}
只是我无法弄清楚如何将它做由于bug in Rakudo。有没有更好的办法,我可以在代码(这就是为什么我写像这样)的C-部分被错误处理?
失败出于同样的原因失败:
class Foo {...}
BEGIN Foo ~~ Bool; # <------
class Foo{
}
问题的部分似乎是Foo
尚未当Pointer.^parameterize
方法被调用组成。
因此,这不是Any
的子类型呢。 (甚至Mu
)
一个解决办法是增加一个.^compose
中的呼叫BEGIN
移相器使用Pointer[::?CLASS]
之前。
class Foo is repr('CPointer') {
BEGIN ::?CLASS.^compose;
my Pointer[::?CLASS] $foo .= new;
}
我的猜测是,真正的解决将是改变Bool.ACCEPTS(Bool:U: \topic)
候选人Bool.ACCEPTS(Bool:U: Mu \topic)
。
究其原因,我认为,这是因为这也失败,基本上是相同的错误:
Mu ~~ Bool