unifiy special handling of params and return
This commit is contained in:
		
							parent
							
								
									92ce27af68
								
							
						
					
					
						commit
						e434130784
					
				
					 8 changed files with 82 additions and 66 deletions
				
			
		| 
						 | 
				
			
			@ -67,7 +67,7 @@ macro_rules! derive_command_from {
 | 
			
		|||
macro_rules! derive_command_into_packet {
 | 
			
		||||
    ($command_type:ident) => {
 | 
			
		||||
        ::paste::paste! {
 | 
			
		||||
            wrap_functions!(associate $command_type;
 | 
			
		||||
            $crate::macros::wrap_functions!(associate $command_type;
 | 
			
		||||
                #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."]
 | 
			
		||||
                ///
 | 
			
		||||
                /// Returns: NULL or a [Packet] containing the command.
 | 
			
		||||
| 
						 | 
				
			
			@ -90,7 +90,7 @@ macro_rules! wrap_command {
 | 
			
		|||
    };
 | 
			
		||||
    ($command:ident) => {
 | 
			
		||||
        ::paste::paste! {
 | 
			
		||||
            wrap_command!($command, [< $command Command >]);
 | 
			
		||||
            $crate::commands::wrap_command!($command, [< $command Command >]);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -109,7 +109,7 @@ wrap_methods!(Bitmap;
 | 
			
		|||
    /// Gets an unsafe reference to the data of the [Bitmap] instance.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the bitmap.
 | 
			
		||||
    mut fn data_ref_mut() -> ByteSlice {
 | 
			
		||||
        return(slice) { unsafe { ByteSlice::from_slice(slice) } };
 | 
			
		||||
    mut fn data_ref_mut(instance) -> ByteSlice {
 | 
			
		||||
        unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -71,8 +71,8 @@ wrap_methods!(DisplayBitVec;
 | 
			
		|||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `index` out of bounds
 | 
			
		||||
    ref fn get(index: usize) -> bool {
 | 
			
		||||
        return(result) { result.map(|x| *x).unwrap_or(false) };
 | 
			
		||||
    ref fn get(instance, index: usize) -> bool {
 | 
			
		||||
         instance.get(index).map(|x| *x).unwrap_or(false)
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of a bit.
 | 
			
		||||
| 
						 | 
				
			
			@ -85,25 +85,25 @@ wrap_methods!(DisplayBitVec;
 | 
			
		|||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `index` out of bounds
 | 
			
		||||
    mut fn set(index: usize, value: bool);
 | 
			
		||||
    mut fn set(instance, index: usize, value: bool);
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of all bits.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `value`: the value to set all bits to
 | 
			
		||||
    mut fn fill(value: bool);
 | 
			
		||||
    mut fn fill(instance, value: bool);
 | 
			
		||||
 | 
			
		||||
    /// Gets the length in bits.
 | 
			
		||||
    ref fn len() -> usize;
 | 
			
		||||
    ref fn len(instance) -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Returns true if length is 0.
 | 
			
		||||
    ref fn is_empty() -> bool;
 | 
			
		||||
    ref fn is_empty(instance) -> bool;
 | 
			
		||||
 | 
			
		||||
    /// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the bitvec.
 | 
			
		||||
    mut fn as_raw_mut_slice() -> ByteSlice {
 | 
			
		||||
        return(slice) { unsafe { ByteSlice::from_slice(slice) } };
 | 
			
		||||
    mut fn as_raw_mut_slice(instance) -> ByteSlice {
 | 
			
		||||
        unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) }
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -77,14 +77,13 @@ wrap_methods!(BrightnessGrid;
 | 
			
		|||
    /// Gets an unsafe reference to the data of the instance.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the grid.
 | 
			
		||||
    mut fn data_ref_mut() -> ByteSlice {
 | 
			
		||||
        return(br_slice) {
 | 
			
		||||
            //noinspection RsAssertEqual
 | 
			
		||||
            const _: () = assert!(size_of::<Brightness>() == 1);
 | 
			
		||||
    mut fn data_ref_mut(instance) -> ByteSlice {
 | 
			
		||||
        //noinspection RsAssertEqual
 | 
			
		||||
        const _: () = assert!(size_of::<Brightness>() == 1);
 | 
			
		||||
 | 
			
		||||
            unsafe {
 | 
			
		||||
                ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
        let br_slice = instance.data_ref_mut();
 | 
			
		||||
        unsafe {
 | 
			
		||||
            ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -72,8 +72,8 @@ wrap_methods!(CharGrid;
 | 
			
		|||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    ref fn get(x: usize, y: usize) -> u32 {
 | 
			
		||||
        return(char) { char as u32 };
 | 
			
		||||
    ref fn get(instance, x: usize, y: usize) -> u32 {
 | 
			
		||||
        instance.get(x, y) as u32
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of the specified position in the grid.
 | 
			
		||||
| 
						 | 
				
			
			@ -89,8 +89,8 @@ wrap_methods!(CharGrid;
 | 
			
		|||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    /// - when providing values that cannot be converted to Rust's `char`.
 | 
			
		||||
    mut fn set(x: usize, y: usize, value: u32) {
 | 
			
		||||
        prepare(value) { char::from_u32(value).unwrap() };
 | 
			
		||||
    mut fn set(instance, x: usize, y: usize, value: u32) {
 | 
			
		||||
        instance.set(x, y, char::from_u32(value).unwrap())
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of all cells in the grid.
 | 
			
		||||
| 
						 | 
				
			
			@ -99,7 +99,7 @@ wrap_methods!(CharGrid;
 | 
			
		|||
    ///
 | 
			
		||||
    /// - `value`: the value to set all cells to
 | 
			
		||||
    /// - when providing values that cannot be converted to Rust's `char`.
 | 
			
		||||
    mut fn fill(value: u32) {
 | 
			
		||||
        prepare(value) { char::from_u32(value).unwrap() };
 | 
			
		||||
    mut fn fill(instance, value: u32) {
 | 
			
		||||
        instance.fill(char::from_u32(value).unwrap())
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -55,7 +55,7 @@ wrap_methods!(Cp437Grid;
 | 
			
		|||
    /// Gets an unsafe reference to the data of the grid.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the instance.
 | 
			
		||||
    mut fn data_ref_mut() -> ByteSlice {
 | 
			
		||||
        return(slice) { unsafe { ByteSlice::from_slice(slice) } };
 | 
			
		||||
    mut fn data_ref_mut(instance) -> ByteSlice {
 | 
			
		||||
        unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,10 +23,10 @@ macro_rules! derive_get_width_height {
 | 
			
		|||
    ($object_type:ident) => {
 | 
			
		||||
        $crate::macros::wrap_methods! {$object_type;
 | 
			
		||||
            /// Gets the width.
 | 
			
		||||
            ref fn width() -> usize;
 | 
			
		||||
            ref fn width(instance) -> usize;
 | 
			
		||||
 | 
			
		||||
            /// Gets the height.
 | 
			
		||||
            ref fn height() -> usize;
 | 
			
		||||
            ref fn height(instance) -> usize;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +45,7 @@ macro_rules! wrap_grid {
 | 
			
		|||
            /// # Panics
 | 
			
		||||
            ///
 | 
			
		||||
            /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
            ref fn get(x: usize, y: usize) -> $value_type;
 | 
			
		||||
            ref fn get(instance, x: usize, y: usize) -> $value_type;
 | 
			
		||||
 | 
			
		||||
            /// Sets the value of the specified position.
 | 
			
		||||
            ///
 | 
			
		||||
| 
						 | 
				
			
			@ -57,14 +57,14 @@ macro_rules! wrap_grid {
 | 
			
		|||
            /// # Panics
 | 
			
		||||
            ///
 | 
			
		||||
            /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
            mut fn set(x: usize, y: usize, value: $value_type);
 | 
			
		||||
            mut fn set(instance, x: usize, y: usize, value: $value_type);
 | 
			
		||||
 | 
			
		||||
            /// Sets the state of all cells in the grid.
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Arguments
 | 
			
		||||
            ///
 | 
			
		||||
            /// - `value`: the value to set all cells to
 | 
			
		||||
            mut fn fill(value: $value_type);
 | 
			
		||||
            mut fn fill(instance, value: $value_type);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,49 +36,66 @@ macro_rules! nonnull_as_mut {
 | 
			
		|||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// meta required on purpose, because otherwise the added documentation would suppress warnings
 | 
			
		||||
macro_rules! wrap_method {
 | 
			
		||||
    (
 | 
			
		||||
        $object_type:ident;
 | 
			
		||||
        $(#[$meta:meta])+
 | 
			
		||||
        $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?)
 | 
			
		||||
        $(-> $return_type:ty)?
 | 
			
		||||
    ) => {
 | 
			
		||||
        ::paste::paste!{
 | 
			
		||||
            $crate::macros::wrap_method!(
 | 
			
		||||
                $object_type;
 | 
			
		||||
                $(#[$meta])+
 | 
			
		||||
                $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?)
 | 
			
		||||
                $(-> $return_type)? {
 | 
			
		||||
                    $instance.$function($($($param_name),*)?)
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    ($object_type:ident;
 | 
			
		||||
        $(#[$meta:meta])+
 | 
			
		||||
        $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?)
 | 
			
		||||
        $(-> $return_type:ty)?
 | 
			
		||||
        $impl:block
 | 
			
		||||
    ) => {
 | 
			
		||||
        paste::paste! {
 | 
			
		||||
            $crate::macros::wrap_functions!([< $object_type:lower >];
 | 
			
		||||
                #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."]
 | 
			
		||||
                ///
 | 
			
		||||
                $(#[$meta])*
 | 
			
		||||
                fn $function(
 | 
			
		||||
                    $instance: ::core::ptr::NonNull<$object_type>
 | 
			
		||||
                    $(,$($param_name: $param_type),*)?
 | 
			
		||||
                ) $(-> $return_type)? {
 | 
			
		||||
                    let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) };
 | 
			
		||||
                    $impl
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
macro_rules! wrap_methods {
 | 
			
		||||
    (
 | 
			
		||||
        $object_type:ident;
 | 
			
		||||
        $(
 | 
			
		||||
            $(#[$meta:meta])+
 | 
			
		||||
            $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*)
 | 
			
		||||
            $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?)
 | 
			
		||||
            $(-> $return_type:ty)?
 | 
			
		||||
            $({
 | 
			
		||||
                $($(prepare($param_let_name:ident) $param_let_expr:block);+;)?
 | 
			
		||||
                $(return($it:ident) $return_expr:block;)?
 | 
			
		||||
            })?
 | 
			
		||||
            ;
 | 
			
		||||
            $($impl:block)?;
 | 
			
		||||
        )+
 | 
			
		||||
    ) => {
 | 
			
		||||
        paste::paste! {
 | 
			
		||||
            $crate::macros::wrap_functions!([< $object_type:lower >];
 | 
			
		||||
            $(
 | 
			
		||||
                #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."]
 | 
			
		||||
                ///
 | 
			
		||||
        $(
 | 
			
		||||
            $crate::macros::wrap_method!($object_type;
 | 
			
		||||
                $(#[$meta])*
 | 
			
		||||
                fn $function(
 | 
			
		||||
                    instance: ::core::ptr::NonNull<$object_type>,
 | 
			
		||||
                    $($param_name: $param_type),*
 | 
			
		||||
                ) $(-> $return_type)? {
 | 
			
		||||
                    let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) };
 | 
			
		||||
                    $($(
 | 
			
		||||
                        $(let $param_let_name = $param_let_expr;)*
 | 
			
		||||
                    )?)?
 | 
			
		||||
                    #[allow(
 | 
			
		||||
                        unused_variables,
 | 
			
		||||
                        reason = "This variable may not be used depending on macro variables" )]
 | 
			
		||||
                    let result = instance.$function($($param_name),*);
 | 
			
		||||
                    $(
 | 
			
		||||
                        $(
 | 
			
		||||
                        let $it = result;
 | 
			
		||||
                        let result = $return_expr;
 | 
			
		||||
                        )?
 | 
			
		||||
                    )?
 | 
			
		||||
                    return result;
 | 
			
		||||
                }
 | 
			
		||||
            )+
 | 
			
		||||
                $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?)
 | 
			
		||||
                $(-> $return_type)?
 | 
			
		||||
                $($impl)?
 | 
			
		||||
            );
 | 
			
		||||
        )+
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -213,5 +230,5 @@ macro_rules! wrap_functions {
 | 
			
		|||
 | 
			
		||||
pub(crate) use {
 | 
			
		||||
    derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields,
 | 
			
		||||
    wrap_functions, wrap_methods, wrap_fields_accessor
 | 
			
		||||
    wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue