add heap_move_ok and heap_move_some helpers
This commit is contained in:
		
							parent
							
								
									0eedbf4a7f
								
							
						
					
					
						commit
						84adf166a9
					
				
					 9 changed files with 55 additions and 89 deletions
				
			
		|  | @ -1,5 +1,8 @@ | |||
| use crate::byte_slice::ByteSlice; | ||||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, SPBitVec}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, | ||||
|     SPBitVec, | ||||
| }; | ||||
| use servicepoint::{ | ||||
|     Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, | ||||
| }; | ||||
|  | @ -33,11 +36,7 @@ pub unsafe extern "C" fn sp_bitmap_new( | |||
|     width: usize, | ||||
|     height: usize, | ||||
| ) -> *mut Bitmap { | ||||
|     if let Some(bitmap) = Bitmap::new(width, height) { | ||||
|         heap_move(bitmap) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_some(Bitmap::new(width, height)) | ||||
| } | ||||
| 
 | ||||
| /// Creates a new [Bitmap] with a size matching the screen.
 | ||||
|  | @ -63,11 +62,7 @@ pub unsafe extern "C" fn sp_bitmap_load( | |||
|     data: ByteSlice, | ||||
| ) -> *mut Bitmap { | ||||
|     let data = unsafe { data.as_slice() }; | ||||
|     if let Ok(bitmap) = Bitmap::load(width, height, data) { | ||||
|         heap_move(bitmap) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_ok(Bitmap::load(width, height, data)) | ||||
| } | ||||
| 
 | ||||
| /// Tries to convert the BitVec to a Bitmap.
 | ||||
|  | @ -81,11 +76,7 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( | |||
|     bitvec: NonNull<SPBitVec>, | ||||
| ) -> *mut Bitmap { | ||||
|     let bitvec = unsafe { heap_remove(bitvec) }; | ||||
|     if let Ok(bitmap) = Bitmap::from_bitvec(width, bitvec.0) { | ||||
|         heap_move(bitmap) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_ok(Bitmap::from_bitvec(width, bitvec.0)) | ||||
| } | ||||
| 
 | ||||
| /// Clones a [Bitmap].
 | ||||
|  | @ -215,12 +206,9 @@ pub unsafe extern "C" fn sp_bitmap_into_packet( | |||
|     compression: CompressionCode, | ||||
| ) -> *mut Packet { | ||||
|     let bitmap = unsafe { heap_remove(bitmap) }; | ||||
|     match Packet::try_from(BitmapCommand { | ||||
|     heap_move_ok(Packet::try_from(BitmapCommand { | ||||
|         bitmap, | ||||
|         origin: Origin::new(x, y), | ||||
|         compression, | ||||
|     }) { | ||||
|         Ok(packet) => heap_move(packet), | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|     } | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,6 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, | ||||
| }; | ||||
| use servicepoint::{ | ||||
|     BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, | ||||
| }; | ||||
|  | @ -162,13 +164,10 @@ pub unsafe extern "C" fn sp_bitvec_into_packet( | |||
|     compression: CompressionCode, | ||||
| ) -> *mut Packet { | ||||
|     let bitvec = unsafe { heap_remove(bitvec) }.0; | ||||
|     match Packet::try_from(BitVecCommand { | ||||
|     heap_move_ok(Packet::try_from(BitVecCommand { | ||||
|         bitvec, | ||||
|         offset, | ||||
|         operation, | ||||
|         compression, | ||||
|     }) { | ||||
|         Ok(packet) => heap_move(packet), | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|     } | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,7 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, | ||||
|     ByteSlice, | ||||
| }; | ||||
| use servicepoint::{ | ||||
|     Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, | ||||
|     Origin, Packet, | ||||
|  | @ -43,13 +46,10 @@ pub unsafe extern "C" fn sp_brightness_grid_load( | |||
|     data: ByteSlice, | ||||
| ) -> *mut BrightnessGrid { | ||||
|     let data = unsafe { data.as_slice() }; | ||||
| 
 | ||||
|     match ByteGrid::load(width, height, data) | ||||
|         .map(move |grid| grid.map(Brightness::saturating_from)) | ||||
|     { | ||||
|         None => std::ptr::null_mut(), | ||||
|         Some(grid) => heap_move(grid), | ||||
|     } | ||||
|     heap_move_some( | ||||
|         ByteGrid::load(width, height, data) | ||||
|             .map(move |grid| grid.map(Brightness::saturating_from)), | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
| /// Clones a [BrightnessGrid].
 | ||||
|  | @ -187,11 +187,8 @@ pub unsafe extern "C" fn sp_brightness_grid_into_packet( | |||
|     y: usize, | ||||
| ) -> *mut Packet { | ||||
|     let grid = unsafe { heap_remove(grid) }; | ||||
|     match Packet::try_from(BrightnessGridCommand { | ||||
|     heap_move_ok(Packet::try_from(BrightnessGridCommand { | ||||
|         grid, | ||||
|         origin: Origin::new(x, y), | ||||
|     }) { | ||||
|         Ok(packet) => heap_move(packet), | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|     } | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,6 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, | ||||
| }; | ||||
| use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; | ||||
| use std::ptr::NonNull; | ||||
| 
 | ||||
|  | @ -32,11 +34,7 @@ pub unsafe extern "C" fn sp_char_grid_load( | |||
|     data: ByteSlice, | ||||
| ) -> *mut CharGrid { | ||||
|     let data = unsafe { data.as_slice() }; | ||||
|     if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) { | ||||
|         heap_move(grid) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) | ||||
| } | ||||
| 
 | ||||
| /// Clones a [CharGrid].
 | ||||
|  | @ -145,11 +143,8 @@ pub unsafe extern "C" fn sp_char_grid_into_packet( | |||
|     y: usize, | ||||
| ) -> *mut Packet { | ||||
|     let grid = unsafe { heap_remove(grid) }; | ||||
|     match Packet::try_from(CharGridCommand { | ||||
|     heap_move_ok(Packet::try_from(CharGridCommand { | ||||
|         grid, | ||||
|         origin: Origin::new(x, y), | ||||
|     }) { | ||||
|         Ok(packet) => heap_move(packet), | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|     } | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,7 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, | ||||
|     ByteSlice, | ||||
| }; | ||||
| use servicepoint::{ | ||||
|     Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, | ||||
| }; | ||||
|  | @ -23,12 +26,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load( | |||
|     data: ByteSlice, | ||||
| ) -> *mut Cp437Grid { | ||||
|     let data = unsafe { data.as_slice() }; | ||||
|     let grid = Cp437Grid::load(width, height, data); | ||||
|     if let Some(grid) = grid { | ||||
|         heap_move(grid) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_some(Cp437Grid::load(width, height, data)) | ||||
| } | ||||
| 
 | ||||
| /// Clones a [Cp437Grid].
 | ||||
|  | @ -147,11 +145,8 @@ pub unsafe extern "C" fn sp_cp437_grid_into_packet( | |||
|     y: usize, | ||||
| ) -> *mut Packet { | ||||
|     let grid = unsafe { heap_remove(grid) }; | ||||
|     match Packet::try_from(Cp437GridCommand { | ||||
|     heap_move_ok(Packet::try_from(Cp437GridCommand { | ||||
|         grid, | ||||
|         origin: Origin::new(x, y), | ||||
|     }) { | ||||
|         Ok(packet) => heap_move(packet), | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|     } | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -60,6 +60,14 @@ pub(crate) fn heap_move_nonnull<T>(x: T) -> NonNull<T> { | |||
|     NonNull::from(Box::leak(Box::new(x))) | ||||
| } | ||||
| 
 | ||||
| pub(crate) fn heap_move_ok<T, E>(x: Result<T, E>) -> *mut T { | ||||
|     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) | ||||
| } | ||||
| 
 | ||||
| pub(crate) fn heap_move_some<T>(x: Option<T>) -> *mut T { | ||||
|     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) | ||||
| } | ||||
| 
 | ||||
| pub(crate) unsafe fn heap_drop<T>(x: NonNull<T>) { | ||||
|     drop(unsafe { heap_remove(x) }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,6 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; | ||||
| use crate::{ | ||||
|     heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, | ||||
| }; | ||||
| use servicepoint::{CommandCode, Header, Packet, TypedCommand}; | ||||
| use std::ptr::NonNull; | ||||
| 
 | ||||
|  | @ -11,11 +13,7 @@ pub unsafe extern "C" fn sp_packet_from_command( | |||
|     command: NonNull<TypedCommand>, | ||||
| ) -> *mut Packet { | ||||
|     let command = unsafe { heap_remove(command) }; | ||||
|     if let Ok(packet) = command.try_into() { | ||||
|         heap_move(packet) | ||||
|     } else { | ||||
|         std::ptr::null_mut() | ||||
|     } | ||||
|     heap_move_ok(command.try_into()) | ||||
| } | ||||
| 
 | ||||
| /// Tries to load a [Packet] from the passed array with the specified length.
 | ||||
|  | @ -24,10 +22,7 @@ pub unsafe extern "C" fn sp_packet_from_command( | |||
| #[no_mangle] | ||||
| pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { | ||||
|     let data = unsafe { data.as_slice() }; | ||||
|     match servicepoint::Packet::try_from(data) { | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|         Ok(packet) => heap_move(packet), | ||||
|     } | ||||
|     heap_move_ok(servicepoint::Packet::try_from(data)) | ||||
| } | ||||
| 
 | ||||
| /// Creates a raw [Packet] from parts.
 | ||||
|  |  | |||
|  | @ -1,4 +1,4 @@ | |||
| use crate::{heap_drop, heap_move, heap_move_nonnull, SPBitVec}; | ||||
| use crate::{heap_drop, heap_move, heap_move_nonnull, heap_move_ok, SPBitVec}; | ||||
| use servicepoint::{ | ||||
|     BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, | ||||
|     CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand, | ||||
|  | @ -15,10 +15,7 @@ pub unsafe extern "C" fn sp_command_try_from_packet( | |||
|     packet: NonNull<Packet>, | ||||
| ) -> *mut TypedCommand { | ||||
|     let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; | ||||
|     match servicepoint::TypedCommand::try_from(packet) { | ||||
|         Err(_) => std::ptr::null_mut(), | ||||
|         Ok(command) => heap_move(command), | ||||
|     } | ||||
|     heap_move_ok(servicepoint::TypedCommand::try_from(packet)) | ||||
| } | ||||
| 
 | ||||
| /// Clones a [TypedCommand] instance.
 | ||||
|  |  | |||
							
								
								
									
										14
									
								
								src/udp.rs
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								src/udp.rs
									
										
									
									
									
								
							|  | @ -1,4 +1,4 @@ | |||
| use crate::{heap_drop, heap_move, heap_remove}; | ||||
| use crate::{heap_drop, heap_move_ok, heap_remove}; | ||||
| use servicepoint::{Header, Packet, TypedCommand, UdpSocketExt}; | ||||
| use std::ffi::{c_char, CStr}; | ||||
| use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; | ||||
|  | @ -20,12 +20,8 @@ pub unsafe extern "C" fn sp_udp_open(host: NonNull<c_char>) -> *mut UdpSocket { | |||
|     let host = unsafe { CStr::from_ptr(host.as_ptr()) } | ||||
|         .to_str() | ||||
|         .expect("Bad encoding"); | ||||
|     let connection = match UdpSocket::bind_connect(host) { | ||||
|         Err(_) => return std::ptr::null_mut(), | ||||
|         Ok(value) => value, | ||||
|     }; | ||||
| 
 | ||||
|     heap_move(connection) | ||||
|     heap_move_ok(UdpSocket::bind_connect(host)) | ||||
| } | ||||
| 
 | ||||
| /// Creates a new instance of [UdpConnection].
 | ||||
|  | @ -48,11 +44,7 @@ pub unsafe extern "C" fn sp_udp_open_ipv4( | |||
|     port: u16, | ||||
| ) -> *mut UdpSocket { | ||||
|     let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); | ||||
|     let connection = match UdpSocket::bind_connect(addr) { | ||||
|         Err(_) => return std::ptr::null_mut(), | ||||
|         Ok(value) => value, | ||||
|     }; | ||||
|     heap_move(connection) | ||||
|     heap_move_ok(UdpSocket::bind_connect(addr)) | ||||
| } | ||||
| 
 | ||||
| /// Sends a [Packet] to the display using the [UdpConnection].
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter