From f8362202590f3d0c06f6e51516e34d6188f700de Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 19 Oct 2024 15:31:54 +0200 Subject: [PATCH] annotate which parameters are consumed, move constants --- crates/servicepoint_binding_c/src/bitmap.rs | 2 + .../src/brightness_grid.rs | 9 +--- crates/servicepoint_binding_c/src/command.rs | 18 ++++++++ .../servicepoint_binding_c/src/connection.rs | 6 +++ .../servicepoint_binding_c/src/constants.rs | 9 +++- .../servicepoint_binding_c/src/cp437_grid.rs | 2 + crates/servicepoint_binding_c/src/packet.rs | 6 ++- .../ServicePoint/BitVec.g.cs | 21 ++++++---- .../ServicePoint/Bitmap.g.cs | 21 ++++++---- .../ServicePoint/BrightnessGrid.g.cs | 32 ++++++--------- .../ServicePoint/Command.g.cs | 41 ++++++++++++++----- .../ServicePoint/Connection.g.cs | 15 +++++-- .../ServicePoint/Constants.cs | 4 ++ .../ServicePoint/Constants.g.cs | 3 ++ .../ServicePoint/Cp437Grid.g.cs | 21 ++++++---- .../ServicePoint/Packet.g.cs | 13 ++++-- .../ServicePoint/ServicePointExtensions.cs | 21 ---------- .../examples/lang_cs/Program.cs | 4 +- crates/servicepoint_csbindgen | 2 +- 19 files changed, 158 insertions(+), 92 deletions(-) delete mode 100644 crates/servicepoint_binding_cs/ServicePoint/ServicePointExtensions.cs diff --git a/crates/servicepoint_binding_c/src/bitmap.rs b/crates/servicepoint_binding_c/src/bitmap.rs index aac605d..433e61e 100644 --- a/crates/servicepoint_binding_c/src/bitmap.rs +++ b/crates/servicepoint_binding_c/src/bitmap.rs @@ -121,6 +121,8 @@ pub unsafe extern "C" fn sp_bitmap_clone( /// - `bitmap` points to a valid [SPBitmap] /// - `bitmap` is not used concurrently or after bitmap call /// - `bitmap` was not passed to another consuming function, e.g. to create a [SPCommand] +/// +/// servicepoint_csbindgen_consumes: bitmap #[no_mangle] pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) { assert!(!bitmap.is_null()); diff --git a/crates/servicepoint_binding_c/src/brightness_grid.rs b/crates/servicepoint_binding_c/src/brightness_grid.rs index 977c976..b4a8c1b 100644 --- a/crates/servicepoint_binding_c/src/brightness_grid.rs +++ b/crates/servicepoint_binding_c/src/brightness_grid.rs @@ -8,13 +8,6 @@ use std::convert::Into; use std::intrinsics::transmute; use std::ptr::NonNull; -/// see [Brightness::MIN] -pub const SP_BRIGHTNESS_MIN: u8 = 0; -/// see [Brightness::MAX] -pub const SP_BRIGHTNESS_MAX: u8 = 11; -/// Count of possible brightness values -pub const SP_BRIGHTNESS_LEVELS: u8 = 12; - /// A grid containing brightness values. /// /// # Examples @@ -133,6 +126,8 @@ pub unsafe extern "C" fn sp_brightness_grid_clone( /// - `brightness_grid` points to a valid [SPBrightnessGrid] /// - `brightness_grid` is not used concurrently or after this call /// - `brightness_grid` was not passed to another consuming function, e.g. to create a [SPCommand] +/// +/// servicepoint_csbindgen_consumes: brightness_grid #[no_mangle] pub unsafe extern "C" fn sp_brightness_grid_free( brightness_grid: *mut SPBrightnessGrid, diff --git a/crates/servicepoint_binding_c/src/command.rs b/crates/servicepoint_binding_c/src/command.rs index 24b733f..e052be9 100644 --- a/crates/servicepoint_binding_c/src/command.rs +++ b/crates/servicepoint_binding_c/src/command.rs @@ -50,6 +50,8 @@ impl Clone for SPCommand { /// - the result is checked for NULL /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: packet #[no_mangle] pub unsafe extern "C" fn sp_command_try_from_packet( packet: *mut SPPacket, @@ -187,6 +189,8 @@ pub unsafe extern "C" fn sp_command_brightness( /// - `grid` is not used concurrently or after this call /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: grid #[no_mangle] pub unsafe extern "C" fn sp_command_char_brightness( x: usize, @@ -227,6 +231,8 @@ pub unsafe extern "C" fn sp_command_char_brightness( /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: bit_vec #[no_mangle] pub unsafe extern "C" fn sp_command_bitmap_linear( offset: usize, @@ -268,6 +274,8 @@ pub unsafe extern "C" fn sp_command_bitmap_linear( /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: bit_vec #[no_mangle] pub unsafe extern "C" fn sp_command_bitmap_linear_and( offset: usize, @@ -309,6 +317,8 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and( /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: bit_vec #[no_mangle] pub unsafe extern "C" fn sp_command_bitmap_linear_or( offset: usize, @@ -350,6 +360,8 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or( /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: bit_vec #[no_mangle] pub unsafe extern "C" fn sp_command_bitmap_linear_xor( offset: usize, @@ -384,6 +396,8 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor( /// - `grid` is not used concurrently or after this call /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: grid #[no_mangle] pub unsafe extern "C" fn sp_command_cp437_data( x: usize, @@ -419,6 +433,8 @@ pub unsafe extern "C" fn sp_command_cp437_data( /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. +/// +/// servicepoint_csbindgen_consumes: bitmap #[no_mangle] pub unsafe extern "C" fn sp_command_bitmap_linear_win( x: usize, @@ -458,6 +474,8 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win( /// - `command` points to a valid [SPCommand] /// - `command` is not used concurrently or after this call /// - `command` was not passed to another consuming function, e.g. to create a [SPPacket] +/// +/// servicepoint_csbindgen_consumes: command #[no_mangle] pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) { assert!(!command.is_null()); diff --git a/crates/servicepoint_binding_c/src/connection.rs b/crates/servicepoint_binding_c/src/connection.rs index 3f54438..4472b8e 100644 --- a/crates/servicepoint_binding_c/src/connection.rs +++ b/crates/servicepoint_binding_c/src/connection.rs @@ -64,6 +64,8 @@ pub unsafe extern "C" fn sp_connection_open( /// - `connection` points to a valid instance of [SPConnection] /// - `packet` points to a valid instance of [SPPacket] /// - `packet` is not used concurrently or after this call +/// +/// servicepoint_csbindgen_consumes: packet #[no_mangle] pub unsafe extern "C" fn sp_connection_send_packet( connection: *const SPConnection, @@ -93,6 +95,8 @@ pub unsafe extern "C" fn sp_connection_send_packet( /// - `connection` points to a valid instance of [SPConnection] /// - `command` points to a valid instance of [SPPacket] /// - `command` is not used concurrently or after this call +/// +/// servicepoint_csbindgen_consumes: packet #[no_mangle] pub unsafe extern "C" fn sp_connection_send_command( connection: *const SPConnection, @@ -116,6 +120,8 @@ pub unsafe extern "C" fn sp_connection_send_command( /// /// - `connection` points to a valid [SPConnection] /// - `connection` is not used concurrently or after this call +/// +/// servicepoint_csbindgen_consumes: connection #[no_mangle] pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) { assert!(!connection.is_null()); diff --git a/crates/servicepoint_binding_c/src/constants.rs b/crates/servicepoint_binding_c/src/constants.rs index 1a268f4..ac30e59 100644 --- a/crates/servicepoint_binding_c/src/constants.rs +++ b/crates/servicepoint_binding_c/src/constants.rs @@ -1,6 +1,6 @@ //! re-exported constants for use in C -use servicepoint::CompressionCode; +use servicepoint::{CompressionCode}; use std::time::Duration; /// size of a single tile in one dimension @@ -24,6 +24,13 @@ pub const SP_PIXEL_COUNT: usize = SP_PIXEL_WIDTH * SP_PIXEL_HEIGHT; /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis(); +/// see [Brightness::MIN] +pub const SP_BRIGHTNESS_MIN: u8 = 0; +/// see [Brightness::MAX] +pub const SP_BRIGHTNESS_MAX: u8 = 11; +/// Count of possible brightness values +pub const SP_BRIGHTNESS_LEVELS: u8 = 12; + /// Specifies the kind of compression to use. #[repr(u16)] pub enum SPCompressionCode { diff --git a/crates/servicepoint_binding_c/src/cp437_grid.rs b/crates/servicepoint_binding_c/src/cp437_grid.rs index e2836ce..a393a98 100644 --- a/crates/servicepoint_binding_c/src/cp437_grid.rs +++ b/crates/servicepoint_binding_c/src/cp437_grid.rs @@ -116,6 +116,8 @@ pub unsafe extern "C" fn sp_cp437_grid_clone( /// - `cp437_grid` points to a valid [SPCp437Grid] /// - `cp437_grid` is not used concurrently or after cp437_grid call /// - `cp437_grid` was not passed to another consuming function, e.g. to create a [SPCommand] +/// +/// servicepoint_csbindgen_consumes: cp437_grid #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) { assert!(!cp437_grid.is_null()); diff --git a/crates/servicepoint_binding_c/src/packet.rs b/crates/servicepoint_binding_c/src/packet.rs index e44c23f..4a574b2 100644 --- a/crates/servicepoint_binding_c/src/packet.rs +++ b/crates/servicepoint_binding_c/src/packet.rs @@ -26,6 +26,8 @@ pub struct SPPacket(pub(crate) servicepoint::packet::Packet); /// - [SPCommand] is not used concurrently or after this call /// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_packet_free`. +/// +/// servicepoint_csbindgen_consumes: command #[no_mangle] pub unsafe extern "C" fn sp_packet_from_command( command: *mut SPCommand, @@ -94,7 +96,7 @@ pub unsafe extern "C" fn sp_packet_clone( /// /// # Panics /// -/// - when `sp_packet_free` is NULL +/// - when `packet` is NULL /// /// # Safety /// @@ -102,6 +104,8 @@ pub unsafe extern "C" fn sp_packet_clone( /// /// - `packet` points to a valid [SPPacket] /// - `packet` is not used concurrently or after this call +/// +/// servicepoint_csbindgen_consumes: packet #[no_mangle] pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) { assert!(!packet.is_null()); diff --git a/crates/servicepoint_binding_cs/ServicePoint/BitVec.g.cs b/crates/servicepoint_binding_cs/ServicePoint/BitVec.g.cs index 1aea6f3..1c62384 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/BitVec.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/BitVec.g.cs @@ -79,7 +79,7 @@ namespace ServicePoint /// public BitVec Clone() { - return new BitVec(BitVec.sp_bitvec_clone(Instance)); + return new BitVec(BitVec.sp_bitvec_clone(this.Instance)); } /// @@ -106,7 +106,7 @@ namespace ServicePoint /// public bool Get(nuint index) { - return BitVec.sp_bitvec_get(Instance, index); + return BitVec.sp_bitvec_get(this.Instance, index); } /// @@ -132,7 +132,7 @@ namespace ServicePoint /// public void Set(nuint index, bool value) { - BitVec.sp_bitvec_set(Instance, index, value); + BitVec.sp_bitvec_set(this.Instance, index, value); } /// @@ -156,7 +156,7 @@ namespace ServicePoint /// public void Fill(bool value) { - BitVec.sp_bitvec_fill(Instance, value); + BitVec.sp_bitvec_fill(this.Instance, value); } /// @@ -178,7 +178,7 @@ namespace ServicePoint /// public nuint Len() { - return BitVec.sp_bitvec_len(Instance); + return BitVec.sp_bitvec_len(this.Instance); } /// @@ -200,7 +200,7 @@ namespace ServicePoint /// public bool IsEmpty() { - return BitVec.sp_bitvec_is_empty(Instance); + return BitVec.sp_bitvec_is_empty(this.Instance); } /// @@ -224,10 +224,11 @@ namespace ServicePoint /// public SPByteSlice UnsafeDataRef() { - return BitVec.sp_bitvec_unsafe_data_ref(Instance); + return BitVec.sp_bitvec_unsafe_data_ref(this.Instance); } +#region internal machinery private SPBitVec* _instance; internal SPBitVec* Instance { @@ -266,8 +267,11 @@ namespace ServicePoint ~BitVec() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_bitvec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPBitVec* sp_bitvec_new(nuint size); @@ -301,6 +305,7 @@ namespace ServicePoint private static extern SPByteSlice sp_bitvec_unsafe_data_ref(SPBitVec* bit_vec); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/Bitmap.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Bitmap.g.cs index 974149f..f58f85c 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Bitmap.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Bitmap.g.cs @@ -86,7 +86,7 @@ namespace ServicePoint /// public Bitmap Clone() { - return new Bitmap(Bitmap.sp_bitmap_clone(Instance)); + return new Bitmap(Bitmap.sp_bitmap_clone(this.Instance)); } /// @@ -111,7 +111,7 @@ namespace ServicePoint /// public bool Get(nuint x, nuint y) { - return Bitmap.sp_bitmap_get(Instance, x, y); + return Bitmap.sp_bitmap_get(this.Instance, x, y); } /// @@ -139,7 +139,7 @@ namespace ServicePoint /// public void Set(nuint x, nuint y, bool value) { - Bitmap.sp_bitmap_set(Instance, x, y, value); + Bitmap.sp_bitmap_set(this.Instance, x, y, value); } /// @@ -163,7 +163,7 @@ namespace ServicePoint /// public void Fill(bool value) { - Bitmap.sp_bitmap_fill(Instance, value); + Bitmap.sp_bitmap_fill(this.Instance, value); } /// @@ -185,7 +185,7 @@ namespace ServicePoint /// public nuint Width() { - return Bitmap.sp_bitmap_width(Instance); + return Bitmap.sp_bitmap_width(this.Instance); } /// @@ -207,7 +207,7 @@ namespace ServicePoint /// public nuint Height() { - return Bitmap.sp_bitmap_height(Instance); + return Bitmap.sp_bitmap_height(this.Instance); } /// @@ -227,10 +227,11 @@ namespace ServicePoint /// public SPByteSlice UnsafeDataRef() { - return Bitmap.sp_bitmap_unsafe_data_ref(Instance); + return Bitmap.sp_bitmap_unsafe_data_ref(this.Instance); } +#region internal machinery private SPBitmap* _instance; internal SPBitmap* Instance { @@ -269,8 +270,11 @@ namespace ServicePoint ~Bitmap() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_bitmap_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPBitmap* sp_bitmap_new(nuint width, nuint height); @@ -303,6 +307,7 @@ namespace ServicePoint private static extern SPByteSlice sp_bitmap_unsafe_data_ref(SPBitmap* bitmap); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/BrightnessGrid.g.cs b/crates/servicepoint_binding_cs/ServicePoint/BrightnessGrid.g.cs index 3c8696b..34bf9b2 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/BrightnessGrid.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/BrightnessGrid.g.cs @@ -10,17 +10,6 @@ using System.Runtime.InteropServices; namespace ServicePoint { - public static unsafe partial class BrightnessGridNative - { - const string __DllName = "servicepoint_binding_c"; - - public const byte SP_BRIGHTNESS_MIN = 0; - public const byte SP_BRIGHTNESS_MAX = 11; - public const byte SP_BRIGHTNESS_LEVELS = 12; - - - - } public unsafe sealed partial class BrightnessGrid: IDisposable { @@ -87,7 +76,7 @@ namespace ServicePoint /// public BrightnessGrid Clone() { - return new BrightnessGrid(BrightnessGrid.sp_brightness_grid_clone(Instance)); + return new BrightnessGrid(BrightnessGrid.sp_brightness_grid_clone(this.Instance)); } /// @@ -114,7 +103,7 @@ namespace ServicePoint /// public byte Get(nuint x, nuint y) { - return BrightnessGrid.sp_brightness_grid_get(Instance, x, y); + return BrightnessGrid.sp_brightness_grid_get(this.Instance, x, y); } /// @@ -143,7 +132,7 @@ namespace ServicePoint /// public void Set(nuint x, nuint y, byte value) { - BrightnessGrid.sp_brightness_grid_set(Instance, x, y, value); + BrightnessGrid.sp_brightness_grid_set(this.Instance, x, y, value); } /// @@ -168,7 +157,7 @@ namespace ServicePoint /// public void Fill(byte value) { - BrightnessGrid.sp_brightness_grid_fill(Instance, value); + BrightnessGrid.sp_brightness_grid_fill(this.Instance, value); } /// @@ -192,7 +181,7 @@ namespace ServicePoint /// public nuint Width() { - return BrightnessGrid.sp_brightness_grid_width(Instance); + return BrightnessGrid.sp_brightness_grid_width(this.Instance); } /// @@ -216,7 +205,7 @@ namespace ServicePoint /// public nuint Height() { - return BrightnessGrid.sp_brightness_grid_height(Instance); + return BrightnessGrid.sp_brightness_grid_height(this.Instance); } /// @@ -242,10 +231,11 @@ namespace ServicePoint /// public SPByteSlice UnsafeDataRef() { - return BrightnessGrid.sp_brightness_grid_unsafe_data_ref(Instance); + return BrightnessGrid.sp_brightness_grid_unsafe_data_ref(this.Instance); } +#region internal machinery private SPBrightnessGrid* _instance; internal SPBrightnessGrid* Instance { @@ -284,8 +274,11 @@ namespace ServicePoint ~BrightnessGrid() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_brightness_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPBrightnessGrid* sp_brightness_grid_new(nuint width, nuint height); @@ -317,6 +310,7 @@ namespace ServicePoint private static extern SPByteSlice sp_brightness_grid_unsafe_data_ref(SPBrightnessGrid* brightness_grid); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/Command.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Command.g.cs index 10aa5e4..107d5e2 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Command.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Command.g.cs @@ -34,10 +34,12 @@ namespace ServicePoint /// - the result is checked for NULL /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: packet /// public static Command? TryFromPacket(Packet packet) { - var native = Command.sp_command_try_from_packet(packet.Instance); + var native = Command.sp_command_try_from_packet(packet.Into()); return native == null ? null : new Command(native); } @@ -61,7 +63,7 @@ namespace ServicePoint /// public Command Clone() { - return new Command(Command.sp_command_clone(Instance)); + return new Command(Command.sp_command_clone(this.Instance)); } /// @@ -165,10 +167,12 @@ namespace ServicePoint /// - `grid` is not used concurrently or after this call /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: grid /// public static Command CharBrightness(nuint x, nuint y, BrightnessGrid grid) { - return new Command(Command.sp_command_char_brightness(x, y, grid.Instance)); + return new Command(Command.sp_command_char_brightness(x, y, grid.Into())); } /// @@ -197,10 +201,12 @@ namespace ServicePoint /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: bit_vec /// public static Command BitmapLinear(nuint offset, BitVec bit_vec, CompressionCode compression) { - return new Command(Command.sp_command_bitmap_linear(offset, bit_vec.Instance, compression)); + return new Command(Command.sp_command_bitmap_linear(offset, bit_vec.Into(), compression)); } /// @@ -229,10 +235,12 @@ namespace ServicePoint /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: bit_vec /// public static Command BitmapLinearAnd(nuint offset, BitVec bit_vec, CompressionCode compression) { - return new Command(Command.sp_command_bitmap_linear_and(offset, bit_vec.Instance, compression)); + return new Command(Command.sp_command_bitmap_linear_and(offset, bit_vec.Into(), compression)); } /// @@ -261,10 +269,12 @@ namespace ServicePoint /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: bit_vec /// public static Command BitmapLinearOr(nuint offset, BitVec bit_vec, CompressionCode compression) { - return new Command(Command.sp_command_bitmap_linear_or(offset, bit_vec.Instance, compression)); + return new Command(Command.sp_command_bitmap_linear_or(offset, bit_vec.Into(), compression)); } /// @@ -293,10 +303,12 @@ namespace ServicePoint /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: bit_vec /// public static Command BitmapLinearXor(nuint offset, BitVec bit_vec, CompressionCode compression) { - return new Command(Command.sp_command_bitmap_linear_xor(offset, bit_vec.Instance, compression)); + return new Command(Command.sp_command_bitmap_linear_xor(offset, bit_vec.Into(), compression)); } /// @@ -318,10 +330,12 @@ namespace ServicePoint /// - `grid` is not used concurrently or after this call /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: grid /// public static Command Cp437Data(nuint x, nuint y, Cp437Grid grid) { - return new Command(Command.sp_command_cp437_data(x, y, grid.Instance)); + return new Command(Command.sp_command_cp437_data(x, y, grid.Into())); } /// @@ -345,13 +359,16 @@ namespace ServicePoint /// - `compression` matches one of the allowed enum values /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_command_free`. + /// + /// servicepoint_csbindgen_consumes: bitmap /// public static Command BitmapLinearWin(nuint x, nuint y, Bitmap bitmap, CompressionCode compression_code) { - return new Command(Command.sp_command_bitmap_linear_win(x, y, bitmap.Instance, compression_code)); + return new Command(Command.sp_command_bitmap_linear_win(x, y, bitmap.Into(), compression_code)); } +#region internal machinery private SPCommand* _instance; internal SPCommand* Instance { @@ -390,8 +407,11 @@ namespace ServicePoint ~Command() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_command_try_from_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPCommand* sp_command_try_from_packet(SPPacket* packet); @@ -435,6 +455,7 @@ namespace ServicePoint private static extern void sp_command_free(SPCommand* command); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs index 543ebd5..169b078 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs @@ -55,10 +55,12 @@ namespace ServicePoint /// - `connection` points to a valid instance of [SPConnection] /// - `packet` points to a valid instance of [SPPacket] /// - `packet` is not used concurrently or after this call + /// + /// servicepoint_csbindgen_consumes: packet /// public bool SendPacket(Packet packet) { - return Connection.sp_connection_send_packet(Instance, packet.Instance); + return Connection.sp_connection_send_packet(this.Instance, packet.Into()); } /// @@ -80,13 +82,16 @@ namespace ServicePoint /// - `connection` points to a valid instance of [SPConnection] /// - `command` points to a valid instance of [SPPacket] /// - `command` is not used concurrently or after this call + /// + /// servicepoint_csbindgen_consumes: packet /// public bool SendCommand(Command command) { - return Connection.sp_connection_send_command(Instance, command.Instance); + return Connection.sp_connection_send_command(this.Instance, command.Instance); } +#region internal machinery private SPConnection* _instance; internal SPConnection* Instance { @@ -125,8 +130,11 @@ namespace ServicePoint ~Connection() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPConnection* sp_connection_open(byte* host); @@ -142,6 +150,7 @@ namespace ServicePoint private static extern void sp_connection_free(SPConnection* connection); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/Constants.cs b/crates/servicepoint_binding_cs/ServicePoint/Constants.cs index dd94fe5..7a13ffa 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Constants.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Constants.cs @@ -19,4 +19,8 @@ public static class Constants /// pixel count on whole screen public const nuint PixelCount = PixelWidth * PixelHeight; + + public const byte MinBrightness = ConstantsNative.SP_BRIGHTNESS_MIN; + public const byte MaxBrightness = ConstantsNative.SP_BRIGHTNESS_MAX; + public const byte BrightnessLevels = ConstantsNative.SP_BRIGHTNESS_LEVELS; } diff --git a/crates/servicepoint_binding_cs/ServicePoint/Constants.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Constants.g.cs index ffc30d4..ba4ba2c 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Constants.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Constants.g.cs @@ -17,6 +17,9 @@ namespace ServicePoint public const nuint SP_TILE_SIZE = 8; public const nuint SP_TILE_WIDTH = 56; public const nuint SP_TILE_HEIGHT = 20; + public const byte SP_BRIGHTNESS_MIN = 0; + public const byte SP_BRIGHTNESS_MAX = 11; + public const byte SP_BRIGHTNESS_LEVELS = 12; diff --git a/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.g.cs index e47922d..a030447 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.g.cs @@ -72,7 +72,7 @@ namespace ServicePoint /// public Cp437Grid Clone() { - return new Cp437Grid(Cp437Grid.sp_cp437_grid_clone(Instance)); + return new Cp437Grid(Cp437Grid.sp_cp437_grid_clone(this.Instance)); } /// @@ -97,7 +97,7 @@ namespace ServicePoint /// public byte Get(nuint x, nuint y) { - return Cp437Grid.sp_cp437_grid_get(Instance, x, y); + return Cp437Grid.sp_cp437_grid_get(this.Instance, x, y); } /// @@ -125,7 +125,7 @@ namespace ServicePoint /// public void Set(nuint x, nuint y, byte value) { - Cp437Grid.sp_cp437_grid_set(Instance, x, y, value); + Cp437Grid.sp_cp437_grid_set(this.Instance, x, y, value); } /// @@ -149,7 +149,7 @@ namespace ServicePoint /// public void Fill(byte value) { - Cp437Grid.sp_cp437_grid_fill(Instance, value); + Cp437Grid.sp_cp437_grid_fill(this.Instance, value); } /// @@ -171,7 +171,7 @@ namespace ServicePoint /// public nuint Width() { - return Cp437Grid.sp_cp437_grid_width(Instance); + return Cp437Grid.sp_cp437_grid_width(this.Instance); } /// @@ -193,7 +193,7 @@ namespace ServicePoint /// public nuint Height() { - return Cp437Grid.sp_cp437_grid_height(Instance); + return Cp437Grid.sp_cp437_grid_height(this.Instance); } /// @@ -215,10 +215,11 @@ namespace ServicePoint /// public SPByteSlice UnsafeDataRef() { - return Cp437Grid.sp_cp437_grid_unsafe_data_ref(Instance); + return Cp437Grid.sp_cp437_grid_unsafe_data_ref(this.Instance); } +#region internal machinery private SPCp437Grid* _instance; internal SPCp437Grid* Instance { @@ -257,8 +258,11 @@ namespace ServicePoint ~Cp437Grid() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_cp437_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPCp437Grid* sp_cp437_grid_new(nuint width, nuint height); @@ -290,6 +294,7 @@ namespace ServicePoint private static extern SPByteSlice sp_cp437_grid_unsafe_data_ref(SPCp437Grid* cp437_grid); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/Packet.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Packet.g.cs index ab6014c..555abd3 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Packet.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Packet.g.cs @@ -32,10 +32,12 @@ namespace ServicePoint /// - [SPCommand] is not used concurrently or after this call /// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or /// by explicitly calling `sp_packet_free`. + /// + /// servicepoint_csbindgen_consumes: command /// public static Packet FromCommand(Command command) { - return new Packet(Packet.sp_packet_from_command(command.Instance)); + return new Packet(Packet.sp_packet_from_command(command.Into())); } /// @@ -82,10 +84,11 @@ namespace ServicePoint /// public Packet Clone() { - return new Packet(Packet.sp_packet_clone(Instance)); + return new Packet(Packet.sp_packet_clone(this.Instance)); } +#region internal machinery private SPPacket* _instance; internal SPPacket* Instance { @@ -124,8 +127,11 @@ namespace ServicePoint ~Packet() => Free(); - const string __DllName = "servicepoint_binding_c"; +#endregion + #nullable restore +#region native methods + const string __DllName = "servicepoint_binding_c"; [DllImport(__DllName, EntryPoint = "sp_packet_from_command", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPPacket* sp_packet_from_command(SPCommand* command); @@ -139,6 +145,7 @@ namespace ServicePoint private static extern void sp_packet_free(SPPacket* packet); +#endregion } [StructLayout(LayoutKind.Sequential)] diff --git a/crates/servicepoint_binding_cs/ServicePoint/ServicePointExtensions.cs b/crates/servicepoint_binding_cs/ServicePoint/ServicePointExtensions.cs deleted file mode 100644 index 9bb8b58..0000000 --- a/crates/servicepoint_binding_cs/ServicePoint/ServicePointExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Diagnostics.CodeAnalysis; - -namespace ServicePoint; - -public static class ServicePointExtensions -{ - public static Packet IntoPacket(this Command command) - { - return Packet.FromCommand(command); - } - - public static bool TryIntoCommand(this Packet packet, [MaybeNullWhen(false)] out Command command) - { - return Command.TryFromPacket(packet, out command); - } - - public unsafe static Span AsSpan(this SPByteSlice slice) - { - return new Span(slice.start, (int)slice.length); - } -} diff --git a/crates/servicepoint_binding_cs/examples/lang_cs/Program.cs b/crates/servicepoint_binding_cs/examples/lang_cs/Program.cs index 234ede8..7c7dbb8 100644 --- a/crates/servicepoint_binding_cs/examples/lang_cs/Program.cs +++ b/crates/servicepoint_binding_cs/examples/lang_cs/Program.cs @@ -7,8 +7,8 @@ if (connection == null) return; } -connection.Send(Command.Clear().IntoPacket()); -connection.Send(Command.Brightness(128).IntoPacket()); +connection.Send(Command.Clear()); +connection.Send(Command.Brightness(Constants.MaxBrightness)); using var pixels = new Bitmap(Constants.PixelWidth, Constants.PixelHeight); diff --git a/crates/servicepoint_csbindgen b/crates/servicepoint_csbindgen index 2dcf44a..de49df9 160000 --- a/crates/servicepoint_csbindgen +++ b/crates/servicepoint_csbindgen @@ -1 +1 @@ -Subproject commit 2dcf44a5c398925249cf96e9bbf724dd551e5b76 +Subproject commit de49df92fa878f0c6685d05ee42f36e3b7d9b042