Skip to content

Commit

Permalink
address Ingvar's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Aug 21, 2024
1 parent 520b9d0 commit c23d960
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
3 changes: 2 additions & 1 deletion crates/bindings-csharp/Runtime/Internal/FFI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ internal static partial class FFI
#endif
;

public enum Errno : ushort
public enum Errno : short
{
EXHAUSTED = -1,
OK = 0,
HOST_CALL_FAILURE = 1,
NO_SUCH_TABLE = 4,
Expand Down
31 changes: 18 additions & 13 deletions crates/bindings-csharp/Runtime/Internal/ITable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Enumerator(FFI.RowIter handle) : IDisposable

public bool MoveNext()
{
if (handle.Equals(FFI.RowIter.INVALID))
if (handle == FFI.RowIter.INVALID)
{
return false;
}
Expand All @@ -31,34 +31,39 @@ public bool MoveNext()
while (true)
{
buffer_len = (uint)buffer.Length;
var ret = FFI._row_iter_bsatn_advance(handle, buffer, ref buffer_len);
if (ret <= 0)
var ret = (FFI.Errno) FFI._row_iter_bsatn_advance(handle, buffer, ref buffer_len);
if (ret == FFI.Errno.EXHAUSTED)
{
Current = new byte[buffer_len];
Array.Copy(buffer, 0, Current, 0, buffer_len);
handle = FFI.RowIter.INVALID;
}
switch (ret)
{
// Iterator exhausted, we're done.
case -1:
handle = FFI.RowIter.INVALID;
case FFI.Errno.EXHAUSTED:
// Iterator advanced but not exhausted,
// we'll need to advance the iterator in the next call to `MoveNext`.
case FFI.Errno.OK:
// Success! Copy over the row data to `Current` from the scratch `buffer`.
Current = new byte[buffer_len];
Array.Copy(buffer, 0, Current, 0, buffer_len);
return buffer_len != 0;
case 0:
return buffer_len != 0;
case (short)(ushort)FFI.Errno.NO_SUCH_ITER:
// Couldn't find the iterator, error!
case FFI.Errno.NO_SUCH_ITER:
throw new NoSuchIterException();
case (short)(ushort)FFI.Errno.BUFFER_TOO_SMALL:
// The scratch `buffer` is too small to fit a row / chunk.
// Grow `buffer` and try again.
case FFI.Errno.BUFFER_TOO_SMALL:
buffer = new byte[buffer_len];
continue;
default:
throw new UnknownException((FFI.Errno)(ushort)ret);
throw new UnknownException(ret);
}
}
}

public void Dispose()
{
if (!handle.Equals(FFI.RowIter.INVALID))
if (handle != FFI.RowIter.INVALID)
{
FFI._row_iter_bsatn_close(handle);
handle = FFI.RowIter.INVALID;
Expand Down
14 changes: 7 additions & 7 deletions crates/bindings-csharp/Runtime/Internal/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,29 @@ private static byte[] Consume(this BytesSource source)
// Write into the spare capacity of the buffer.
var spare = buffer.AsSpan((int)written);
var buf_len = (uint)spare.Length;
var ret = FFI._bytes_source_read(source, spare, ref buf_len);
var ret = (FFI.Errno)FFI._bytes_source_read(source, spare, ref buf_len);
written += buf_len;
switch (ret)
{
// Host side source exhausted, we're done.
case -1:
case FFI.Errno.EXHAUSTED:
Array.Resize(ref buffer, (int)written);
return buffer;
// Wrote the entire spare capacity.
// Need to reserve more space in the buffer.
case 0 when written == buffer.Length:
case FFI.Errno.OK when written == buffer.Length:
Array.Resize(ref buffer, buffer.Length + 1024);
break;
// Host didn't write as much as possible.
// Try to read some more.
// The host will likely not trigger this branch (current host doesn't),
// but a module should be prepared for it.
case 0:
case FFI.Errno.OK:
break;
case (short)(ushort)FFI.Errno.NO_SUCH_BYTES:
case FFI.Errno.NO_SUCH_BYTES:
throw new NoSuchBytesException();
default:
throw new UnknownException((FFI.Errno)(ushort)ret);
throw new UnknownException(ret);
}
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ BytesSink error
var error_str = e.ToString();
var error_bytes = System.Text.Encoding.UTF8.GetBytes(error_str);
error.Write(error_bytes);
return (short)(ushort)FFI.Errno.HOST_CALL_FAILURE;
return (short)FFI.Errno.HOST_CALL_FAILURE;
}
}
}
2 changes: 1 addition & 1 deletion crates/bindings-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ impl RowIter {
let buf_ptr = buf.spare_capacity_mut();
let mut buf_len = buf_ptr.len();
let ret = unsafe { raw::_row_iter_bsatn_advance(self.raw, buf_ptr.as_mut_ptr().cast(), &mut buf_len) };
if ret <= 0 {
if let -1 | 0 = ret {
// SAFETY: `_row_iter_bsatn_advance` just wrote `buf_len` bytes into the end of `buf`.
unsafe { buf.set_len(buf.len() + buf_len) };
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c23d960

Please sign in to comment.