@@ -92,7 +92,12 @@ impl Foo {
9292 #[ inline( always) ]
9393 pub fn set_voltage ( & mut self , value : f32 ) -> Result < ( ) , CanError > {
9494 #[ cfg( feature = "range_checked" ) ]
95- assert ! ( 0f32 <= value && value <= 63.9990234375f32 ) ;
95+ if value < 0f32 || 63.9990234375f32 < value {
96+ return Err ( CanError :: ParameterOutOfRange {
97+ min : 0f64 ,
98+ max : 63.9990234375f64 ,
99+ } ) ;
100+ }
96101 let factor = 0.000976562_f32 ;
97102 let offset = 0_f32 ;
98103 let value = ( ( value - offset) / factor) as u16 ;
@@ -135,7 +140,12 @@ impl Foo {
135140 #[ inline( always) ]
136141 pub fn set_current ( & mut self , value : f32 ) -> Result < ( ) , CanError > {
137142 #[ cfg( feature = "range_checked" ) ]
138- assert ! ( -2048f32 <= value && value <= 2047.9375f32 ) ;
143+ if value < -2048f32 || 2047.9375f32 < value {
144+ return Err ( CanError :: ParameterOutOfRange {
145+ min : -2048f64 ,
146+ max : 2047.9375f64 ,
147+ } ) ;
148+ }
139149 let factor = 0.0625_f32 ;
140150 let offset = 0_f32 ;
141151 let value = ( ( value - offset) / factor) as i16 ;
@@ -220,7 +230,12 @@ impl Bar {
220230 #[ inline( always) ]
221231 pub fn set_one ( & mut self , value : u8 ) -> Result < ( ) , CanError > {
222232 #[ cfg( feature = "range_checked" ) ]
223- assert ! ( 0u8 <= value && value <= 3u8 ) ;
233+ if value < 0u8 || 3u8 < value {
234+ return Err ( CanError :: ParameterOutOfRange {
235+ min : 0f64 ,
236+ max : 3f64 ,
237+ } ) ;
238+ }
224239 let start_bit = 15 ;
225240 let bits = 2 ;
226241 value. pack_be_bits ( & mut self . raw , start_bit, bits) ;
@@ -259,7 +274,12 @@ impl Bar {
259274 #[ inline( always) ]
260275 pub fn set_two ( & mut self , value : f32 ) -> Result < ( ) , CanError > {
261276 #[ cfg( feature = "range_checked" ) ]
262- assert ! ( 0f32 <= value && value <= 100f32 ) ;
277+ if value < 0f32 || 100f32 < value {
278+ return Err ( CanError :: ParameterOutOfRange {
279+ min : 0f64 ,
280+ max : 100f64 ,
281+ } ) ;
282+ }
263283 let factor = 0.39_f32 ;
264284 let offset = 0_f32 ;
265285 let value = ( ( value - offset) / factor) as u8 ;
@@ -306,7 +326,12 @@ impl Bar {
306326 #[ inline( always) ]
307327 pub fn set_three ( & mut self , value : u8 ) -> Result < ( ) , CanError > {
308328 #[ cfg( feature = "range_checked" ) ]
309- assert ! ( 0u8 <= value && value <= 7u8 ) ;
329+ if value < 0u8 || 7u8 < value {
330+ return Err ( CanError :: ParameterOutOfRange {
331+ min : 0f64 ,
332+ max : 7f64 ,
333+ } ) ;
334+ }
310335 let start_bit = 13 ;
311336 let bits = 3 ;
312337 value. pack_be_bits ( & mut self . raw , start_bit, bits) ;
@@ -349,7 +374,12 @@ impl Bar {
349374 #[ inline( always) ]
350375 pub fn set_four ( & mut self , value : u8 ) -> Result < ( ) , CanError > {
351376 #[ cfg( feature = "range_checked" ) ]
352- assert ! ( 0u8 <= value && value <= 3u8 ) ;
377+ if value < 0u8 || 3u8 < value {
378+ return Err ( CanError :: ParameterOutOfRange {
379+ min : 0f64 ,
380+ max : 3f64 ,
381+ } ) ;
382+ }
353383 let start_bit = 10 ;
354384 let bits = 2 ;
355385 value. pack_be_bits ( & mut self . raw , start_bit, bits) ;
@@ -395,9 +425,17 @@ pub enum BarFour {
395425/// This is just to make testing easier
396426fn main ( ) { }
397427
398- #[ derive( Clone , Copy , PartialEq , Eq ) ]
428+ #[ derive( Clone , Copy , PartialEq ) ]
399429#[ cfg_attr( feature = "debug" , derive( Debug ) ) ]
400430pub enum CanError {
401431 UnknownMessageId ( u32 ) ,
432+ /// Signal parameter is not within the range
433+ /// defined in the dbc
434+ ParameterOutOfRange {
435+ /// Minimum value defined in DBC
436+ min : f64 ,
437+ /// Maximum value defined in DBC
438+ max : f64 ,
439+ } ,
402440 InvalidPayloadSize ,
403441}
0 commit comments