cargo fmt

This commit is contained in:
Vinzenz Schroeter 2024-05-16 23:18:43 +02:00
parent 972d713cf1
commit 8307354a58
14 changed files with 293 additions and 100 deletions

View file

@ -23,7 +23,14 @@ fn main() {
loop {
connection
.send(Command::BitmapLinearWin(Origin::top_left(), field.clone(), CompressionCode::Bzip2).into())
.send(
Command::BitmapLinearWin(
Origin::top_left(),
field.clone(),
CompressionCode::Bzip2,
)
.into(),
)
.expect("could not send");
thread::sleep(Duration::from_millis(30));
field = iteration(field);
@ -37,7 +44,10 @@ fn iteration(field: PixelGrid) -> PixelGrid {
let old_state = field.get(x, y);
let neighbors = count_neighbors(&field, x as i32, y as i32);
let new_state = matches!((old_state, neighbors), (true, 2) | (true, 3) | (false, 3));
let new_state = matches!(
(old_state, neighbors),
(true, 2) | (true, 3) | (false, 3)
);
next.set(x, y, new_state);
}
}

View file

@ -3,7 +3,10 @@ use std::time::Duration;
use clap::Parser;
use servicepoint2::{Command, CompressionCode, Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
use servicepoint2::{
Command, CompressionCode, Connection, Origin, PixelGrid, PIXEL_HEIGHT,
PIXEL_WIDTH,
};
#[derive(Parser, Debug)]
struct Cli {
@ -24,7 +27,14 @@ fn main() {
pixels.set((y + x_offset) % PIXEL_WIDTH as usize, y, true);
}
connection
.send(Command::BitmapLinearWin(Origin::top_left(), pixels.clone(), CompressionCode::Lzma).into())
.send(
Command::BitmapLinearWin(
Origin::top_left(),
pixels.clone(),
CompressionCode::Lzma,
)
.into(),
)
.unwrap();
thread::sleep(Duration::from_millis(14));
}

View file

@ -3,8 +3,11 @@ use std::time::Duration;
use clap::Parser;
use rand::Rng;
use servicepoint2::{
ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT,
TILE_WIDTH,
};
use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness};
use servicepoint2::{ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT, TILE_WIDTH};
#[derive(Parser, Debug)]
struct Cli {
@ -27,9 +30,13 @@ fn main() {
if cli.enable_all {
let mut filled_grid = PixelGrid::max_sized();
filled_grid.fill(true);
connection
.send(BitmapLinearWin(Origin::top_left(), filled_grid, CompressionCode::Lzma).into())
.unwrap();
let command = BitmapLinearWin(
Origin::top_left(),
filled_grid,
CompressionCode::Lzma,
);
connection.send(command.into()).expect("send failed");
}
// set all pixels to the same random brightness
@ -54,7 +61,9 @@ fn main() {
}
}
connection.send(CharBrightness(origin, luma).into()).unwrap();
connection
.send(CharBrightness(origin, luma).into())
.unwrap();
std::thread::sleep(wait_duration);
}
}

View file

@ -3,7 +3,10 @@ use std::time::Duration;
use clap::Parser;
use servicepoint2::{BitVec, Command, CompressionCode, Connection, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
use servicepoint2::{
BitVec, Command, CompressionCode, Connection, PixelGrid, PIXEL_HEIGHT,
PIXEL_WIDTH,
};
#[derive(Parser, Debug)]
struct Cli {
@ -34,7 +37,10 @@ fn main() {
let bit_vec = BitVec::from(&*pixel_data);
connection
.send(Command::BitmapLinearAnd(0, bit_vec, CompressionCode::Lzma).into())
.send(
Command::BitmapLinearAnd(0, bit_vec, CompressionCode::Lzma)
.into(),
)
.unwrap();
thread::sleep(sleep_duration);
}

View file

@ -0,0 +1 @@

View file

@ -135,7 +135,10 @@ pub mod c_api {
/// Loads a `BitVec` from the provided data.
/// The returned instance has to be freed with `bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_load(data: *const u8, data_length: usize) -> *mut BitVec {
pub unsafe extern "C" fn sp2_bit_vec_load(
data: *const u8,
data_length: usize,
) -> *mut BitVec {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(BitVec::from(data)))
}
@ -143,7 +146,9 @@ pub mod c_api {
/// Clones a `BitVec`.
/// The returned instance has to be freed with `bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_clone(this: *const BitVec) -> *mut BitVec {
pub unsafe extern "C" fn sp2_bit_vec_clone(
this: *const BitVec,
) -> *mut BitVec {
Box::into_raw(Box::new((*this).clone()))
}
@ -157,13 +162,20 @@ pub mod c_api {
/// Gets the value of a bit from the `BitVec`.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_get(this: *const BitVec, index: usize) -> bool {
pub unsafe extern "C" fn sp2_bit_vec_get(
this: *const BitVec,
index: usize,
) -> bool {
(*this).get(index)
}
/// Sets the value of a bit in the `BitVec`.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_set(this: *mut BitVec, index: usize, value: bool) -> bool {
pub unsafe extern "C" fn sp2_bit_vec_set(
this: *mut BitVec,
index: usize,
value: bool,
) -> bool {
(*this).set(index, value)
}
@ -195,7 +207,9 @@ pub mod c_api {
/// Reading and writing concurrently to either the original instance or the returned data will
/// result in undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_unsafe_data_ref(this: *mut BitVec) -> CByteSlice {
pub unsafe extern "C" fn sp2_bit_vec_unsafe_data_ref(
this: *mut BitVec,
) -> CByteSlice {
let data = (*this).mut_data_ref();
CByteSlice {
start: data.as_mut_ptr_range().start,

View file

@ -67,21 +67,28 @@ impl From<ByteGrid> for Vec<u8> {
}
#[cfg(feature = "c_api")]
pub mod c_api
{
pub mod c_api {
use crate::{ByteGrid, CByteSlice};
/// Creates a new `ByteGrid` instance.
/// The returned instance has to be freed with `byte_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_new(width: usize, height: usize) -> *mut ByteGrid {
pub unsafe extern "C" fn sp2_byte_grid_new(
width: usize,
height: usize,
) -> *mut ByteGrid {
Box::into_raw(Box::new(ByteGrid::new(width, height)))
}
/// Loads a `ByteGrid` with the specified dimensions from the provided data.
/// The returned instance has to be freed with `byte_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut ByteGrid {
pub unsafe extern "C" fn sp2_byte_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
) -> *mut ByteGrid {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(ByteGrid::load(width, height, data)))
}
@ -89,7 +96,9 @@ pub mod c_api
/// Clones a `ByteGrid`.
/// The returned instance has to be freed with `byte_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_clone(this: *const ByteGrid) -> *mut ByteGrid {
pub unsafe extern "C" fn sp2_byte_grid_clone(
this: *const ByteGrid,
) -> *mut ByteGrid {
Box::into_raw(Box::new((*this).clone()))
}
@ -103,31 +112,47 @@ pub mod c_api
/// Get the current value at the specified position
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_get(this: *const ByteGrid, x: usize, y: usize) -> u8 {
pub unsafe extern "C" fn sp2_byte_grid_get(
this: *const ByteGrid,
x: usize,
y: usize,
) -> u8 {
(*this).get(x, y)
}
/// Sets the current value at the specified position
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_set(this: *mut ByteGrid, x: usize, y: usize, value: u8) {
pub unsafe extern "C" fn sp2_byte_grid_set(
this: *mut ByteGrid,
x: usize,
y: usize,
value: u8,
) {
(*this).set(x, y, value);
}
/// Fills the whole `ByteGrid` with the specified value
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_fill(this: *mut ByteGrid, value: u8) {
pub unsafe extern "C" fn sp2_byte_grid_fill(
this: *mut ByteGrid,
value: u8,
) {
(*this).fill(value);
}
/// Gets the width in pixels of the `ByteGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_width(this: *const ByteGrid) -> usize {
pub unsafe extern "C" fn sp2_byte_grid_width(
this: *const ByteGrid,
) -> usize {
(*this).width
}
/// Gets the height in pixels of the `ByteGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_height(this: *const ByteGrid) -> usize {
pub unsafe extern "C" fn sp2_byte_grid_height(
this: *const ByteGrid,
) -> usize {
(*this).height
}
@ -141,11 +166,13 @@ pub mod c_api
/// Reading and writing concurrently to either the original instance or the returned data will
/// result in undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_unsafe_data_ref(this: *mut ByteGrid) -> CByteSlice {
pub unsafe extern "C" fn sp2_byte_grid_unsafe_data_ref(
this: *mut ByteGrid,
) -> CByteSlice {
let data = (*this).mut_data_ref();
CByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}
}
}

View file

@ -1,6 +1,8 @@
use crate::{BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE};
use crate::command_code::CommandCode;
use crate::compression::{into_compressed, into_decompressed};
use crate::{
BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE,
};
/// An origin marks the top left position of a window sent to the display.
#[derive(Debug, Clone, Copy)]
@ -68,8 +70,13 @@ impl From<Command> for Packet {
command_code_only(CommandCode::BitmapLegacy)
}
Command::CharBrightness(Origin(x, y), grid) => Packet(
Header(CommandCode::CharBrightness.into(),
x, y, grid.width as u16, grid.height as u16),
Header(
CommandCode::CharBrightness.into(),
x,
y,
grid.width as u16,
grid.height as u16,
),
grid.into(),
),
Command::Brightness(brightness) => Packet(
@ -82,7 +89,11 @@ impl From<Command> for Packet {
),
vec![brightness],
),
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels, compression) => {
Command::BitmapLinearWin(
Origin(pixel_x, pixel_y),
pixels,
compression,
) => {
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
@ -91,7 +102,9 @@ impl From<Command> for Packet {
let pixel_h = pixels.height as u16;
let payload = into_compressed(compression, pixels.into());
let command = match compression {
CompressionCode::Uncompressed => CommandCode::BitmapLinearWinUncompressed,
CompressionCode::Uncompressed => {
CommandCode::BitmapLinearWinUncompressed
}
CompressionCode::Zlib => CommandCode::BitmapLinearWinZlib,
CompressionCode::Bzip2 => CommandCode::BitmapLinearWinBzip2,
CompressionCode::Lzma => CommandCode::BitmapLinearWinLzma,
@ -136,7 +149,13 @@ impl From<Command> for Packet {
)
}
Command::Cp437Data(Origin(x, y), grid) => Packet(
Header(CommandCode::Cp437Data.into(), x, y, grid.width as u16, grid.height as u16),
Header(
CommandCode::Cp437Data.into(),
x,
y,
grid.width as u16,
grid.height as u16,
),
grid.into(),
),
}
@ -253,7 +272,10 @@ impl TryFrom<Packet> for Command {
}
}
fn packet_into_bitmap_win(packet: Packet, compression: CompressionCode) -> Result<Command, TryFromPacketError> {
fn packet_into_bitmap_win(
packet: Packet,
compression: CompressionCode,
) -> Result<Command, TryFromPacketError> {
let Packet(Header(_, tiles_x, pixels_y, tile_w, pixel_h), payload) = packet;
let payload = match into_decompressed(compression, payload) {
@ -282,13 +304,7 @@ fn bitmap_linear_into_packet(
let length = payload.len() as u16;
let payload = into_compressed(compression, payload);
Packet(
Header(
command.into(),
offset,
length,
compression.into(),
0,
),
Header(command.into(), offset, length, compression.into(), 0),
payload,
)
}
@ -336,17 +352,21 @@ fn packet_into_linear_bitmap(
}
#[cfg(feature = "c_api")]
pub mod c_api
{
pub mod c_api {
use std::ptr::null_mut;
use crate::{BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin, Packet, PixelGrid};
use crate::{
BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin,
Packet, PixelGrid,
};
/// Tries to turn a `Packet` into a `Command`. The packet is gets deallocated in the process.
///
/// Returns: pointer to command or NULL
#[no_mangle]
pub unsafe extern "C" fn sp2_command_try_from_packet(packet: *mut Packet) -> *mut Command {
pub unsafe extern "C" fn sp2_command_try_from_packet(
packet: *mut Packet,
) -> *mut Command {
let packet = *Box::from_raw(packet);
match Command::try_from(packet) {
Err(_) => null_mut(),
@ -356,7 +376,9 @@ pub mod c_api
/// Clones a `Command` instance
#[no_mangle]
pub unsafe extern "C" fn sp2_command_clone(original: *const Command) -> *mut Command {
pub unsafe extern "C" fn sp2_command_clone(
original: *const Command,
) -> *mut Command {
Box::into_raw(Box::new((*original).clone()))
}
@ -380,54 +402,99 @@ pub mod c_api
/// Allocates a new `Command::Brightness` instance
#[no_mangle]
pub unsafe extern "C" fn sp2_command_brightness(brightness: Brightness) -> *mut Command {
pub unsafe extern "C" fn sp2_command_brightness(
brightness: Brightness,
) -> *mut Command {
Box::into_raw(Box::new(Command::Brightness(brightness)))
}
/// Allocates a new `Command::CharBrightness` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_char_brightness(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
pub unsafe extern "C" fn sp2_command_char_brightness(
x: u16,
y: u16,
byte_grid: *mut ByteGrid,
) -> *mut Command {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::CharBrightness(Origin(x, y), byte_grid)))
Box::into_raw(Box::new(Command::CharBrightness(
Origin(x, y),
byte_grid,
)))
}
/// Allocates a new `Command::BitmapLinear` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
pub unsafe extern "C" fn sp2_command_bitmap_linear(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinear(offset, bit_vec, compression)))
Box::into_raw(Box::new(Command::BitmapLinear(
offset,
bit_vec,
compression,
)))
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear_and(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
pub unsafe extern "C" fn sp2_command_bitmap_linear_and(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearAnd(offset, bit_vec, compression)))
Box::into_raw(Box::new(Command::BitmapLinearAnd(
offset,
bit_vec,
compression,
)))
}
/// Allocates a new `Command::BitmapLinearOr` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear_or(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
pub unsafe extern "C" fn sp2_command_bitmap_linear_or(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearOr(offset, bit_vec, compression)))
Box::into_raw(Box::new(Command::BitmapLinearOr(
offset,
bit_vec,
compression,
)))
}
/// Allocates a new `Command::BitmapLinearXor` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear_xor(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
pub unsafe extern "C" fn sp2_command_bitmap_linear_xor(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearXor(offset, bit_vec, compression)))
Box::into_raw(Box::new(Command::BitmapLinearXor(
offset,
bit_vec,
compression,
)))
}
/// Allocates a new `Command::Cp437Data` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_cp437_data(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
pub unsafe extern "C" fn sp2_command_cp437_data(
x: u16,
y: u16,
byte_grid: *mut ByteGrid,
) -> *mut Command {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::Cp437Data(Origin(x, y), byte_grid)))
}
@ -435,9 +502,18 @@ pub mod c_api
/// Allocates a new `Command::BitmapLinearWin` instance.
/// The passed `PixelGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear_win(x: u16, y: u16, byte_grid: *mut PixelGrid, compression_code: CompressionCode) -> *mut Command {
pub unsafe extern "C" fn sp2_command_bitmap_linear_win(
x: u16,
y: u16,
byte_grid: *mut PixelGrid,
compression_code: CompressionCode,
) -> *mut Command {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::BitmapLinearWin(Origin(x, y), byte_grid, compression_code)))
Box::into_raw(Box::new(Command::BitmapLinearWin(
Origin(x, y),
byte_grid,
compression_code,
)))
}
/// Deallocates a `Command`. Note that connection_send does this implicitly, so you only need
@ -446,4 +522,4 @@ pub mod c_api
pub unsafe extern "C" fn sp2_command_dealloc(ptr: *mut Command) {
_ = Box::from_raw(ptr);
}
}
}

View file

@ -45,7 +45,9 @@ impl TryFrom<u16> for CommandCode {
#[allow(deprecated)]
value if value == BitmapLegacy as u16 => Ok(BitmapLegacy),
value if value == BitmapLinear as u16 => Ok(BitmapLinear),
value if value == BitmapLinearWinUncompressed as u16 => Ok(BitmapLinearWinUncompressed),
value if value == BitmapLinearWinUncompressed as u16 => {
Ok(BitmapLinearWinUncompressed)
}
value if value == BitmapLinearAnd as u16 => Ok(BitmapLinearAnd),
value if value == BitmapLinearOr as u16 => Ok(BitmapLinearOr),
value if value == BitmapLinearXor as u16 => Ok(BitmapLinearXor),

View file

@ -21,7 +21,11 @@ pub(crate) fn into_decompressed(
let mut decompress = flate2::Decompress::new(true);
let mut buffer = [0u8; 10000];
let status = match decompress.decompress(&payload, &mut buffer, FlushDecompress::Finish) {
let status = match decompress.decompress(
&payload,
&mut buffer,
FlushDecompress::Finish,
) {
Err(_) => return None,
Ok(status) => status,
};
@ -29,7 +33,9 @@ pub(crate) fn into_decompressed(
match status {
Status::Ok => None,
Status::BufError => None,
Status::StreamEnd => Some(buffer[0..(decompress.total_out() as usize)].to_owned()),
Status::StreamEnd => Some(
buffer[0..(decompress.total_out() as usize)].to_owned(),
),
}
}
#[cfg(feature = "compression_bzip2")]
@ -42,9 +48,7 @@ pub(crate) fn into_decompressed(
}
}
#[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => {
Some(lzma::decompress(&payload).unwrap())
}
CompressionCode::Lzma => Some(lzma::decompress(&payload).unwrap()),
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(&*payload) {
@ -68,10 +72,14 @@ pub(crate) fn into_compressed(
CompressionCode::Uncompressed => payload,
#[cfg(feature = "compression_zlib")]
CompressionCode::Zlib => {
let mut compress = flate2::Compress::new(flate2::Compression::fast(), true);
let mut compress =
flate2::Compress::new(flate2::Compression::fast(), true);
let mut buffer = [0u8; 10000];
match compress.compress(&payload, &mut buffer, FlushCompress::Finish).expect("compress failed") {
match compress
.compress(&payload, &mut buffer, FlushCompress::Finish)
.expect("compress failed")
{
Status::Ok => panic!("buffer should be big enough"),
Status::BufError => panic!("BufError"),
Status::StreamEnd => {}
@ -89,9 +97,7 @@ pub(crate) fn into_compressed(
}
}
#[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => {
lzma::compress(&payload, 6).unwrap()
}
CompressionCode::Lzma => lzma::compress(&payload, 6).unwrap(),
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => {
let mut encoder =

View file

@ -54,10 +54,7 @@ impl Connection {
/// connection.send(servicepoint2::Command::BitmapLinearWin(servicepoint2::Origin::top_left(), pixels, CompressionCode::Lzma).into())
/// .expect("send failed");
/// ```
pub fn send(
&self,
packet: Packet,
) -> Result<(), std::io::Error> {
pub fn send(&self, packet: Packet) -> Result<(), std::io::Error> {
debug!("sending {packet:?}");
let data: Vec<u8> = packet.into();
self.socket.send(&data)?;
@ -66,8 +63,7 @@ impl Connection {
}
#[cfg(feature = "c_api")]
pub mod c_api
{
pub mod c_api {
use std::ffi::{c_char, CStr};
use std::ptr::null_mut;
@ -80,11 +76,13 @@ pub mod c_api
///
/// Panics: bad string encoding
#[no_mangle]
pub unsafe extern "C" fn sp2_connection_open(host: *const c_char) -> *mut Connection {
pub unsafe extern "C" fn sp2_connection_open(
host: *const c_char,
) -> *mut Connection {
let host = CStr::from_ptr(host).to_str().expect("Bad encoding");
let connection = match Connection::open(host) {
Err(_) => return null_mut(),
Ok(value) => value
Ok(value) => value,
};
Box::into_raw(Box::new(connection))
@ -92,7 +90,10 @@ pub mod c_api
/// Sends the command instance. The instance is consumed / destroyed and cannot be used after this call.
#[no_mangle]
pub unsafe extern "C" fn sp2_connection_send(connection: *const Connection, command_ptr: *mut Packet) -> bool{
pub unsafe extern "C" fn sp2_connection_send(
connection: *const Connection,
command_ptr: *mut Packet,
) -> bool {
let packet = Box::from_raw(command_ptr);
(*connection).send(*packet).is_ok()
}
@ -102,4 +103,4 @@ pub mod c_api
pub unsafe extern "C" fn sp2_connection_dealloc(ptr: *mut Connection) {
_ = Box::from_raw(ptr);
}
}
}

View file

@ -11,6 +11,7 @@ pub use crate::c_slice::CByteSlice;
mod bit_vec;
mod byte_grid;
mod c_slice;
mod command;
mod command_code;
mod compression;
@ -18,7 +19,6 @@ mod compression_code;
mod connection;
mod packet;
mod pixel_grid;
mod c_slice;
/// size of a single tile in one dimension
pub const TILE_SIZE: u16 = 8;

View file

@ -66,22 +66,26 @@ mod c_api {
/// Turns a `Command` into a `Packet`. The command gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_packet_from_command(command: *mut Command) -> *mut Packet {
pub unsafe extern "C" fn sp2_packet_from_command(
command: *mut Command,
) -> *mut Packet {
let command = *Box::from_raw(command);
let packet = command.into();
Box::into_raw(Box::new(packet))
}
/// Tries to load a `Packet` from the passed array with the specified length.
///
/// returns: NULL in case of an error, pointer to the allocated packet otherwise
#[no_mangle]
pub unsafe extern "C" fn sp2_packet_try_load(data: *const u8, length: usize) -> *mut Packet {
pub unsafe extern "C" fn sp2_packet_try_load(
data: *const u8,
length: usize,
) -> *mut Packet {
let data = std::slice::from_raw_parts(data, length);
match Packet::try_from(data) {
Err(_) => null_mut(),
Ok(packet) => Box::into_raw(Box::new(packet))
Ok(packet) => Box::into_raw(Box::new(packet)),
}
}
@ -92,4 +96,4 @@ mod c_api {
pub unsafe extern "C" fn sp2_packet_dealloc(this: *mut Packet) {
_ = Box::from_raw(this)
}
}
}

View file

@ -90,22 +90,29 @@ impl From<PixelGrid> for Vec<u8> {
}
#[cfg(feature = "c_api")]
pub mod c_api
{
pub mod c_api {
use crate::c_slice::CByteSlice;
use crate::PixelGrid;
/// Creates a new `PixelGrid` instance.
/// The returned instance has to be freed with `pixel_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_new(width: usize, height: usize) -> *mut PixelGrid {
pub unsafe extern "C" fn sp2_pixel_grid_new(
width: usize,
height: usize,
) -> *mut PixelGrid {
Box::into_raw(Box::new(PixelGrid::new(width, height)))
}
/// Loads a `PixelGrid` with the specified dimensions from the provided data.
/// The returned instance has to be freed with `pixel_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut PixelGrid {
pub unsafe extern "C" fn sp2_pixel_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
) -> *mut PixelGrid {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(PixelGrid::load(width, height, data)))
}
@ -113,7 +120,9 @@ pub mod c_api
/// Clones a `PixelGrid`.
/// The returned instance has to be freed with `pixel_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_clone(this: *const PixelGrid) -> *mut PixelGrid {
pub unsafe extern "C" fn sp2_pixel_grid_clone(
this: *const PixelGrid,
) -> *mut PixelGrid {
Box::into_raw(Box::new((*this).clone()))
}
@ -127,31 +136,47 @@ pub mod c_api
/// Get the current value at the specified position
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_get(this: *const PixelGrid, x: usize, y: usize) -> bool {
pub unsafe extern "C" fn sp2_pixel_grid_get(
this: *const PixelGrid,
x: usize,
y: usize,
) -> bool {
(*this).get(x, y)
}
/// Sets the current value at the specified position
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_set(this: *mut PixelGrid, x: usize, y: usize, value: bool) {
pub unsafe extern "C" fn sp2_pixel_grid_set(
this: *mut PixelGrid,
x: usize,
y: usize,
value: bool,
) {
(*this).set(x, y, value);
}
/// Fills the whole `PixelGrid` with the specified value
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_fill(this: *mut PixelGrid, value: bool) {
pub unsafe extern "C" fn sp2_pixel_grid_fill(
this: *mut PixelGrid,
value: bool,
) {
(*this).fill(value);
}
/// Gets the width in pixels of the `PixelGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_width(this: *const PixelGrid) -> usize {
pub unsafe extern "C" fn sp2_pixel_grid_width(
this: *const PixelGrid,
) -> usize {
(*this).width
}
/// Gets the height in pixels of the `PixelGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_height(this: *const PixelGrid) -> usize {
pub unsafe extern "C" fn sp2_pixel_grid_height(
this: *const PixelGrid,
) -> usize {
(*this).height
}
@ -165,7 +190,9 @@ pub mod c_api
/// Reading and writing concurrently to either the original instance or the returned data will
/// result in undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_unsafe_data_ref(this: *mut PixelGrid) -> CByteSlice {
pub unsafe extern "C" fn sp2_pixel_grid_unsafe_data_ref(
this: *mut PixelGrid,
) -> CByteSlice {
let data = (*this).mut_data_ref();
CByteSlice {
start: data.as_mut_ptr_range().start,