re-export everything from top-level for nicer docs

This commit is contained in:
Vinzenz Schroeter 2024-09-07 12:23:32 +02:00
parent e97418b51b
commit eddeb2ea2d
12 changed files with 83 additions and 52 deletions

View file

@ -17,18 +17,18 @@ This crate contains C bindings for the `servicepoint` library, enabling users to
#include "servicepoint.h" #include "servicepoint.h"
int main(void) { int main(void) {
sp_Connection *connection = sp_connection_open("localhost:2342"); SPConnection *connection = sp_connection_open("172.23.42.29:2342");
if (connection == NULL) if (connection == NULL)
return 1; return 1;
sp_PixelGrid *pixels = sp_pixel_grid_new(sp_PIXEL_WIDTH, sp_PIXEL_HEIGHT); SPPixelGrid *pixels = sp_pixel_grid_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
sp_pixel_grid_fill(pixels, true); sp_pixel_grid_fill(pixels, true);
sp_Command *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed); SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
sp_Packet *packet = sp_packet_from_command(command); SPPacket *packet = sp_packet_from_command(command);
if (!sp_connection_send(connection, packet)) while (sp_connection_send(connection, sp_packet_clone(packet)));
return 1;
sp_packet_dealloc(packet);
sp_connection_dealloc(connection); sp_connection_dealloc(connection);
return 0; return 0;
} }
@ -53,7 +53,7 @@ You have the choice of linking statically (recommended) or dynamically.
## Notes on differences to rust library ## Notes on differences to rust library
- function names are: `sp_` \<struct_name\> \<rust name\>. - function names are: `sp_` \<struct_name\> \<rust name\>.
- Instances get consumed in the same way they do when writing rust / C# code. Do not use an instance after an (implicit!) free. - Instances get consumed in the same way they do when writing rust code. Do not use an instance after an (implicit!) free.
- Option<T> or Result<T, E> turn into nullable return values - check for NULL! - Option<T> or Result<T, E> turn into nullable return values - check for NULL!
- There are no specifics for C++ here yet. You might get a nicer header when generating directly for C++, but it should be usable. - There are no specifics for C++ here yet. You might get a nicer header when generating directly for C++, but it should be usable.
- Reading and writing to instances concurrently is not safe. Only reading concurrently is safe. - Reading and writing to instances concurrently is not safe. Only reading concurrently is safe.

View file

@ -167,6 +167,8 @@ typedef struct SPPixelGrid SPPixelGrid;
/** /**
* Represents a span of memory (`&mut [u8]` ) as a struct usable by C code. * Represents a span of memory (`&mut [u8]` ) as a struct usable by C code.
* *
* You should not create an instance of this type in your C code.
*
* # Safety * # Safety
* *
* The caller has to make sure that: * The caller has to make sure that:
@ -174,6 +176,8 @@ typedef struct SPPixelGrid SPPixelGrid;
* - accesses to the memory pointed to with `start` is never accessed outside `length` * - accesses to the memory pointed to with `start` is never accessed outside `length`
* - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in * - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
* the function returning this type. * the function returning this type.
* - an instance of this created from C is never passed to a consuming function, as the rust code
* will try to free the memory of a potentially separate allocator.
*/ */
typedef struct SPByteSlice { typedef struct SPByteSlice {
/** /**
@ -186,11 +190,6 @@ typedef struct SPByteSlice {
size_t length; size_t length;
} SPByteSlice; } SPByteSlice;
/**
* Type alias for documenting the meaning of the variable in enum values
*/
typedef size_t SPOffset;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // __cplusplus #endif // __cplusplus
@ -556,7 +555,7 @@ size_t sp_brightness_grid_width(const struct SPBrightnessGrid *this_);
* - the returned `Command` instance is freed in some way, either by using a consuming function or * - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`. * by explicitly calling `sp_command_dealloc`.
*/ */
struct SPCommand *sp_command_bitmap_linear(SPOffset offset, struct SPCommand *sp_command_bitmap_linear(size_t offset,
struct SPBitVec *bit_vec, struct SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
@ -581,7 +580,7 @@ struct SPCommand *sp_command_bitmap_linear(SPOffset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or * - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`. * by explicitly calling `sp_command_dealloc`.
*/ */
struct SPCommand *sp_command_bitmap_linear_and(SPOffset offset, struct SPCommand *sp_command_bitmap_linear_and(size_t offset,
struct SPBitVec *bit_vec, struct SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
@ -606,7 +605,7 @@ struct SPCommand *sp_command_bitmap_linear_and(SPOffset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or * - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`. * by explicitly calling `sp_command_dealloc`.
*/ */
struct SPCommand *sp_command_bitmap_linear_or(SPOffset offset, struct SPCommand *sp_command_bitmap_linear_or(size_t offset,
struct SPBitVec *bit_vec, struct SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
@ -652,7 +651,7 @@ struct SPCommand *sp_command_bitmap_linear_win(size_t x,
* - the returned `Command` instance is freed in some way, either by using a consuming function or * - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`. * by explicitly calling `sp_command_dealloc`.
*/ */
struct SPCommand *sp_command_bitmap_linear_xor(SPOffset offset, struct SPCommand *sp_command_bitmap_linear_xor(size_t offset,
struct SPBitVec *bit_vec, struct SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);

View file

@ -2,7 +2,7 @@
//! //!
//! prefix `sp_bit_vec_` //! prefix `sp_bit_vec_`
use crate::c_slice::SPByteSlice; use crate::SPByteSlice;
use servicepoint::bitvec::prelude::{BitVec, Msb0}; use servicepoint::bitvec::prelude::{BitVec, Msb0};
/// A vector of bits /// A vector of bits

View file

@ -2,7 +2,7 @@
//! //!
//! prefix `sp_brightness_grid_` //! prefix `sp_brightness_grid_`
use crate::c_slice::SPByteSlice; use crate::SPByteSlice;
use servicepoint::{Brightness, DataRef, Grid, PrimitiveGrid}; use servicepoint::{Brightness, DataRef, Grid, PrimitiveGrid};
use std::intrinsics::transmute; use std::intrinsics::transmute;

View file

@ -3,6 +3,8 @@
#[repr(C)] #[repr(C)]
/// Represents a span of memory (`&mut [u8]` ) as a struct usable by C code. /// Represents a span of memory (`&mut [u8]` ) as a struct usable by C code.
/// ///
/// You should not create an instance of this type in your C code.
///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
@ -10,6 +12,8 @@
/// - accesses to the memory pointed to with `start` is never accessed outside `length` /// - accesses to the memory pointed to with `start` is never accessed outside `length`
/// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in /// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
/// the function returning this type. /// the function returning this type.
/// - an instance of this created from C is never passed to a consuming function, as the rust code
/// will try to free the memory of a potentially separate allocator.
pub struct SPByteSlice { pub struct SPByteSlice {
/// The start address of the memory /// The start address of the memory
pub start: *mut u8, pub start: *mut u8,

View file

@ -6,13 +6,10 @@ use std::ptr::null_mut;
use servicepoint::{Brightness, Origin}; use servicepoint::{Brightness, Origin};
use crate::bit_vec::SPBitVec; use crate::{
use crate::brightness_grid::SPBrightnessGrid; SPBitVec, SPBrightnessGrid, SPCompressionCode, SPCp437Grid, SPPacket,
use crate::constants::SPCompressionCode; SPPixelGrid,
use crate::cp437_grid::SPCp437Grid; };
use crate::packet::SPPacket;
use crate::pixel_grid::SPPixelGrid;
use crate::SPOffset;
/// A low-level display command. /// A low-level display command.
/// ///
@ -196,7 +193,7 @@ pub unsafe extern "C" fn sp_command_char_brightness(
/// by explicitly calling `sp_command_dealloc`. /// by explicitly calling `sp_command_dealloc`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear( pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: SPOffset, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
@ -229,7 +226,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
/// by explicitly calling `sp_command_dealloc`. /// by explicitly calling `sp_command_dealloc`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_and( pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: SPOffset, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
@ -262,7 +259,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
/// by explicitly calling `sp_command_dealloc`. /// by explicitly calling `sp_command_dealloc`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_or( pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: SPOffset, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
@ -295,7 +292,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
/// by explicitly calling `sp_command_dealloc`. /// by explicitly calling `sp_command_dealloc`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_xor( pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: SPOffset, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {

View file

@ -5,7 +5,7 @@
use std::ffi::{c_char, CStr}; use std::ffi::{c_char, CStr};
use std::ptr::null_mut; use std::ptr::null_mut;
use crate::packet::SPPacket; use crate::SPPacket;
/// A connection to the display. /// A connection to the display.
/// ///

View file

@ -2,8 +2,8 @@
//! //!
//! prefix `sp_cp437_grid_` //! prefix `sp_cp437_grid_`
use crate::c_slice::SPByteSlice; use crate::SPByteSlice;
use servicepoint::{Cp437Grid, DataRef, Grid}; use servicepoint::{DataRef, Grid};
/// A C-wrapper for grid containing codepage 437 characters. /// A C-wrapper for grid containing codepage 437 characters.
/// ///
@ -18,7 +18,7 @@ use servicepoint::{Cp437Grid, DataRef, Grid};
/// sp_cp437_grid_dealloc(grid); /// sp_cp437_grid_dealloc(grid);
/// ``` /// ```
pub struct SPCp437Grid { pub struct SPCp437Grid {
pub(crate) actual: Cp437Grid, pub(crate) actual: servicepoint::Cp437Grid,
} }
impl Clone for SPCp437Grid { impl Clone for SPCp437Grid {
@ -45,7 +45,7 @@ pub unsafe extern "C" fn sp_cp437_grid_new(
height: usize, height: usize,
) -> *mut SPCp437Grid { ) -> *mut SPCp437Grid {
Box::into_raw(Box::new(SPCp437Grid { Box::into_raw(Box::new(SPCp437Grid {
actual: Cp437Grid::new(width, height), actual: servicepoint::Cp437Grid::new(width, height),
})) }))
} }
@ -72,7 +72,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
) -> *mut SPCp437Grid { ) -> *mut SPCp437Grid {
let data = std::slice::from_raw_parts(data, data_length); let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(SPCp437Grid { Box::into_raw(Box::new(SPCp437Grid {
actual: Cp437Grid::load(width, height, data), actual: servicepoint::Cp437Grid::load(width, height, data),
})) }))
} }

View file

@ -1,24 +1,55 @@
//! C API wrapper for the `servicepoint` crate. //! C API wrapper for the [servicepoint](https://docs.rs/servicepoint/latest/servicepoint/) crate.
//!
//! # Examples
//!
//! Make sure to check out [this GitHub repo](https://github.com/arfst23/ServicePoint) as well!
//!
//! ```C
//! #include <stdio.h>
//! #include "servicepoint.h"
//!
//! int main(void) {
//! SPConnection *connection = sp_connection_open("172.23.42.29:2342");
//! if (connection == NULL)
//! return 1;
//!
//! SPPixelGrid *pixels = sp_pixel_grid_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
//! sp_pixel_grid_fill(pixels, true);
//!
//! SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
//! SPPacket *packet = sp_packet_from_command(command);
//! while (sp_connection_send(connection, sp_packet_clone(packet)));
//!
//! sp_packet_dealloc(packet);
//! sp_connection_dealloc(connection);
//! return 0;
//! }
//! ```
pub use crate::c_slice::SPByteSlice; pub use bit_vec::*;
pub use brightness_grid::*;
pub use byte_slice::*;
pub use command::*;
pub use connection::*;
pub use constants::*;
pub use cp437_grid::*;
pub use packet::*;
pub use pixel_grid::*;
pub mod bit_vec; mod bit_vec;
pub mod brightness_grid; mod brightness_grid;
pub mod command; mod command;
pub mod connection; mod connection;
pub mod packet; mod packet;
pub mod pixel_grid; mod pixel_grid;
pub mod c_slice; mod byte_slice;
pub mod cp437_grid; mod cp437_grid;
pub mod constants; mod constants;
/// Type alias for documenting the meaning of the variable in enum values
pub type SPOffset = usize;

View file

@ -4,7 +4,7 @@
use std::ptr::null_mut; use std::ptr::null_mut;
use crate::command::SPCommand; use crate::SPCommand;
/// The raw packet /// The raw packet
pub struct SPPacket(pub(crate) servicepoint::Packet); pub struct SPPacket(pub(crate) servicepoint::Packet);

View file

@ -4,7 +4,7 @@
use servicepoint::{DataRef, Grid}; use servicepoint::{DataRef, Grid};
use crate::c_slice::SPByteSlice; use crate::byte_slice::SPByteSlice;
/// A grid of pixels. /// A grid of pixels.
/// ///

View file

@ -11,7 +11,7 @@ fn main() {
.input_extern_file("../servicepoint_binding_c/src/connection.rs") .input_extern_file("../servicepoint_binding_c/src/connection.rs")
.input_extern_file("../servicepoint_binding_c/src/pixel_grid.rs") .input_extern_file("../servicepoint_binding_c/src/pixel_grid.rs")
.input_extern_file("../servicepoint_binding_c/src/lib.rs") .input_extern_file("../servicepoint_binding_c/src/lib.rs")
.input_extern_file("../servicepoint_binding_c/src/c_slice.rs") .input_extern_file("../servicepoint_binding_c/src/byte_slice.rs")
.input_extern_file("../servicepoint_binding_c/src/packet.rs") .input_extern_file("../servicepoint_binding_c/src/packet.rs")
.input_extern_file("../servicepoint_binding_c/src/constants.rs") .input_extern_file("../servicepoint_binding_c/src/constants.rs")
.csharp_dll_name("servicepoint_binding_c") .csharp_dll_name("servicepoint_binding_c")