diff --git a/crates/bindings-csharp/Runtime/Internal/FFI.cs b/crates/bindings-csharp/Runtime/Internal/FFI.cs index 5e85c4171b..c10396a97b 100644 --- a/crates/bindings-csharp/Runtime/Internal/FFI.cs +++ b/crates/bindings-csharp/Runtime/Internal/FFI.cs @@ -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, diff --git a/crates/bindings-csharp/Runtime/Internal/ITable.cs b/crates/bindings-csharp/Runtime/Internal/ITable.cs index 70e6ad6b47..d790036568 100644 --- a/crates/bindings-csharp/Runtime/Internal/ITable.cs +++ b/crates/bindings-csharp/Runtime/Internal/ITable.cs @@ -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; } @@ -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; diff --git a/crates/bindings-csharp/Runtime/Internal/Module.cs b/crates/bindings-csharp/Runtime/Internal/Module.cs index 9e60a77835..631852bc1b 100644 --- a/crates/bindings-csharp/Runtime/Internal/Module.cs +++ b/crates/bindings-csharp/Runtime/Internal/Module.cs @@ -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); } } } @@ -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; } } } diff --git a/crates/bindings-sys/src/lib.rs b/crates/bindings-sys/src/lib.rs index e563598648..e8db076dc8 100644 --- a/crates/bindings-sys/src/lib.rs +++ b/crates/bindings-sys/src/lib.rs @@ -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) }; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/insert_primitives_as_strings_reducer.rs b/crates/sdk/tests/test-client/src/module_bindings/insert_primitives_as_strings_reducer.rs index bdf1d86dc5..e8fdb704c6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/insert_primitives_as_strings_reducer.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/insert_primitives_as_strings_reducer.rs @@ -15,7 +15,7 @@ use spacetimedb_sdk::{ #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] pub struct InsertPrimitivesAsStringsArgs { - pub s: EveryPrimitiveStruct, + pub t: EveryPrimitiveStruct, } impl Reducer for InsertPrimitivesAsStringsArgs { @@ -23,8 +23,8 @@ impl Reducer for InsertPrimitivesAsStringsArgs { } #[allow(unused)] -pub fn insert_primitives_as_strings(s: EveryPrimitiveStruct) { - InsertPrimitivesAsStringsArgs { s }.invoke(); +pub fn insert_primitives_as_strings(t: EveryPrimitiveStruct) { + InsertPrimitivesAsStringsArgs { t }.invoke(); } #[allow(unused)] @@ -32,8 +32,8 @@ pub fn on_insert_primitives_as_strings( mut __callback: impl FnMut(&Identity, Option
, &Status, &EveryPrimitiveStruct) + Send + 'static, ) -> ReducerCallbackId { InsertPrimitivesAsStringsArgs::on_reducer(move |__identity, __addr, __status, __args| { - let InsertPrimitivesAsStringsArgs { s } = __args; - __callback(__identity, __addr, __status, s); + let InsertPrimitivesAsStringsArgs { t } = __args; + __callback(__identity, __addr, __status, t); }) } @@ -42,8 +42,8 @@ pub fn once_on_insert_primitives_as_strings( __callback: impl FnOnce(&Identity, Option
, &Status, &EveryPrimitiveStruct) + Send + 'static, ) -> ReducerCallbackId { InsertPrimitivesAsStringsArgs::once_on_reducer(move |__identity, __addr, __status, __args| { - let InsertPrimitivesAsStringsArgs { s } = __args; - __callback(__identity, __addr, __status, s); + let InsertPrimitivesAsStringsArgs { t } = __args; + __callback(__identity, __addr, __status, t); }) }