alef 0.62.4

Opinionated polyglot binding generator for Rust libraries
Documentation
    /// <summary>
    /// Register a handler for {{ method_name }}.
    /// </summary>
    public int {{ method_name }}({% if metadata_params %}{{ metadata_params }}, {% endif %}Func<string, string> handler) {
{{ declarations }}
        ArgumentNullException.ThrowIfNull(handler);
        var handle = GCHandle.Alloc(handler, GCHandleType.Normal);
        IntPtr ctx = GCHandle.ToIntPtr(handle);
        int result;
        using var ownerLease = BorrowOwnerHandle();
        try {
{{ setup }}
            result = NativeMethods.{{ native_method }}(
                ownerLease.Handle,
                _handlerCallback,
                _handlerResponseFree,
                ctx{{ arg_lines }}
            );
        } catch {
            handle.Free();
            throw;
        } finally {
{{ teardown }}
        }

        if (result == 0) {
            // Keep the GCHandle alive for the lifetime of the registration
            lock (_registeredCallbacks) {
                _registeredCallbacks[ctx] = handle;
            }
        } else {
            handle.Free();
        }
        return result;
    }