From 05aff8b2dd9d192ec45a22c3d68b81f73c80861e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 20 Oct 2024 12:29:08 +0200 Subject: [PATCH] add fake connection to C API --- .../servicepoint_binding_c/src/connection.rs | 16 ++++++++++++++ .../ServicePoint/Connection.g.cs | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/crates/servicepoint_binding_c/src/connection.rs b/crates/servicepoint_binding_c/src/connection.rs index 6edb804..d99a7bd 100644 --- a/crates/servicepoint_binding_c/src/connection.rs +++ b/crates/servicepoint_binding_c/src/connection.rs @@ -46,6 +46,22 @@ pub unsafe extern "C" fn sp_connection_open( Box::into_raw(Box::new(SPConnection(connection))) } +/// Creates a new instance of [SPConnection] for testing that does not actually send anything. +/// +/// returns: a new instance. Will never return NULL. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - the returned instance is freed in some way, either by using a consuming function or +/// by explicitly calling `sp_connection_free`. +#[no_mangle] +pub unsafe extern "C" fn sp_connection_fake() -> NonNull { + let result = Box::new(SPConnection(servicepoint::Connection::Fake)); + NonNull::from(Box::leak(result)) +} + /// Sends a [SPPacket] to the display using the [SPConnection]. /// /// The passed `packet` gets consumed. diff --git a/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs b/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs index a397c89..ea9b6ec 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/Connection.g.cs @@ -37,6 +37,24 @@ namespace ServicePoint return native == null ? null : new Connection(native); } + /// + /// Creates a new instance of [SPConnection] for testing that does not actually send anything. + /// + /// returns: a new instance. Will never return NULL. + /// + /// # Safety + /// + /// The caller has to make sure that: + /// + /// - the returned instance is freed in some way, either by using a consuming function or + /// by explicitly calling `sp_connection_free`. + /// + [System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + public static Connection Fake() + { + return new Connection(Connection.sp_connection_fake()); + } + /// /// Sends a [SPPacket] to the display using the [SPConnection]. /// @@ -141,6 +159,9 @@ namespace ServicePoint [DllImport(__DllName, EntryPoint = "sp_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] private static extern SPConnection* sp_connection_open(byte* host); + [DllImport(__DllName, EntryPoint = "sp_connection_fake", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + private static extern SPConnection* sp_connection_fake(); + [DllImport(__DllName, EntryPoint = "sp_connection_send_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [return: MarshalAs(UnmanagedType.U1)] private static extern bool sp_connection_send_packet(SPConnection* connection, SPPacket* packet);