interoptopus_csharp 0.16.0-alpha.13

The C# backend for Interoptopus.
Documentation
{%- include "rust/pattern/vec/common_fields.cs" %}

{%- include "rust/pattern/vec/common_body.cs" %}

/// A Rust-allocated growable array of <c>{{ element_type }}</c> (marshalled elements).
///
/// Elements are marshalled from their unmanaged representation on each access.
{{ _types_docs_owned }}
[NativeMarshalling(typeof(MarshallerMeta))]
public partial class {{ name }} : IDisposable
{

    /// Creates a new Rust-owned vector by marshalling each element from the given span.
    {{ _fns_decorators_all | indent }}
    public static unsafe {{ name }} From(Span<{{ element_type }}> _data)
    {
        var _temp = new {{ unmanaged_element_type }}[_data.Length];
        for (var i = 0; i < _data.Length; ++i)
        {
            _temp[i] = _data[i].IntoUnmanaged();
        }
        fixed (void* _data_ptr = _temp)
        {
            InteropHelper.interoptopus_vec_create((IntPtr) _data_ptr, (ulong)_data.Length, out var _out);
            return _out.IntoManaged();
        }
    }

    /// Gets the element at the given index, marshalling from its unmanaged form.
    public unsafe {{ element_type }} this[int i]
    {
        {{ _fns_decorators_all | indent(prefix="        ") }}
        get
        {
            if (i >= Count) throw new IndexOutOfRangeException();
            if (_ptr == IntPtr.Zero) throw new NullReferenceException();
            var _element = Marshal.PtrToStructure<{{ unmanaged_element_type }}>(new IntPtr(_ptr.ToInt64() + i * sizeof({{ unmanaged_element_type }})));
            return _element.IntoManaged();
        }
    }
}

/// Convenience extension to convert a <c>{{ element_type }}[]</c> array to a <see cref="{{ name }}"/>.
public static class {{ name }}Extensions
{
    /// Marshals the array into a new Rust-owned <see cref="{{ name }}"/>.
    /// Call <see cref="{{ name }}.Dispose"/> if the value is not passed back to Rust.
    {{ _fns_decorators_all | indent }}
    public static {{ name }} IntoVec(this {{ element_type }}[] s) { return {{ name }}.From(s); }
}