public static async IAsyncEnumerable<{{ item_type }}> {{ method_name }}({{ params_decl }})
{
{{ setup_code }} var iterHandle = NativeMethods.{{ start_method }}({{ native_args }});
if (iterHandle == 0) throw GetLastError();
try
{
while (true)
{
var itemHandle = NativeMethods.{{ next_method }}(iterHandle);
if (itemHandle == 0) break;
try
{
var jsonPtr = NativeMethods.{{ item_to_json }}(itemHandle);
var json = global::System.Runtime.InteropServices.Marshal.PtrToStringUTF8(jsonPtr);
NativeMethods.FreeString(jsonPtr);
if (!string.IsNullOrEmpty(json))
{
var item = JsonSerializer.Deserialize<{{ item_type }}>(json, JsonOptions)!;
yield return item;
}
}
finally
{
NativeMethods.{{ item_free }}(itemHandle);
}
}
}
finally
{
NativeMethods.{{ free_method }}(iterHandle);
{{ cleanup_code }}
}
}