Skip to content

missing lifetime specifier in pointer #82

Open
@crackcomm

Description

@crackcomm

I stumbled onto a bizarre error raised by the borrow checker if at least one of the function arguments is a reference and return type is ocaml::Pointer.

Example code that raised the error:

struct Keypair(schnorrkel::Keypair);
ocaml::custom!(Keypair);
struct Signature(schnorrkel::Signature);
ocaml::custom!(Signature);

#[ocaml::func]
pub unsafe fn keypair_sign(
    keypair: ocaml::Pointer<Keypair>,
    ctx: &[u8],
    bytes: &[u8],
) -> ocaml::Pointer<Signature> {
    let ctx = schnorrkel::signing_context(ctx);
    let signature = keypair.as_ref().0.sign(ctx.bytes(bytes));
    ocaml::Pointer::alloc_custom(Signature(signature))
}

It's not possible to make keypair_sign<'a> (a hack anyways) since OCaml functions may not contain generics but it is possible to hack using in_band_lifetimes feature:

#![feature(in_band_lifetimes)]

struct Keypair(schnorrkel::Keypair);
ocaml::custom!(Keypair);
struct Signature(schnorrkel::Signature);
ocaml::custom!(Signature);

#[ocaml::func]
pub unsafe fn keypair_sign(
    keypair: ocaml::Pointer<Keypair>,
    ctx: &'a [u8],
    bytes: &[u8],
) -> ocaml::Pointer<'a, Signature> {
    let ctx = schnorrkel::signing_context(ctx);
    let signature = keypair.as_ref().0.sign(ctx.bytes(bytes));
    ocaml::Pointer::alloc_custom(Signature(signature))
}

This error is not raised when arguments are Vec<u8> etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions