motorcortex_rust/
set_parameter_value.rs

1pub trait SetParameterValue {
2    fn to_bytes_as_bool(&self) -> Vec<u8>;
3    fn to_bytes_as_i8(&self) -> Vec<u8>;
4    fn to_bytes_as_u8(&self) -> Vec<u8>;
5    fn to_bytes_as_i16(&self) -> Vec<u8>;
6    fn to_bytes_as_u16(&self) -> Vec<u8>;
7    fn to_bytes_as_i32(&self) -> Vec<u8>;
8    fn to_bytes_as_u32(&self) -> Vec<u8>;
9    fn to_bytes_as_f32(&self) -> Vec<u8>;
10    fn to_bytes_as_i64(&self) -> Vec<u8>;
11    fn to_bytes_as_u64(&self) -> Vec<u8>;
12    fn to_bytes_as_f64(&self) -> Vec<u8>;
13    fn to_bytes_as_string(&self) -> Vec<u8>;
14}
15
16// Value
17macro_rules! impl_set_parameter_value {
18    ($t:ty) => {
19        impl SetParameterValue for $t {
20            fn to_bytes_as_bool(&self) -> Vec<u8> {
21                vec![(*self != Default::default()) as u8]
22            }
23            fn to_bytes_as_i8(&self) -> Vec<u8> {
24                (*self as i8).to_le_bytes().to_vec()
25            }
26            fn to_bytes_as_u8(&self) -> Vec<u8> {
27                (*self as u8).to_le_bytes().to_vec()
28            }
29            fn to_bytes_as_i16(&self) -> Vec<u8> {
30                (*self as i16).to_le_bytes().to_vec()
31            }
32            fn to_bytes_as_u16(&self) -> Vec<u8> {
33                (*self as u16).to_le_bytes().to_vec()
34            }
35            fn to_bytes_as_i32(&self) -> Vec<u8> {
36                (*self as i32).to_le_bytes().to_vec()
37            }
38            fn to_bytes_as_u32(&self) -> Vec<u8> {
39                (*self as u32).to_le_bytes().to_vec()
40            }
41            fn to_bytes_as_f32(&self) -> Vec<u8> {
42                (*self as f32).to_le_bytes().to_vec()
43            }
44            fn to_bytes_as_i64(&self) -> Vec<u8> {
45                (*self as i64).to_le_bytes().to_vec()
46            }
47            fn to_bytes_as_u64(&self) -> Vec<u8> {
48                (*self as u64).to_le_bytes().to_vec()
49            }
50            fn to_bytes_as_f64(&self) -> Vec<u8> {
51                (*self as f64).to_le_bytes().to_vec()
52            }
53            fn to_bytes_as_string(&self) -> Vec<u8> {
54                self.to_string().as_bytes().to_vec()
55            }
56        }
57    };
58}
59// Specialization for `bool`
60impl SetParameterValue for bool {
61    fn to_bytes_as_bool(&self) -> Vec<u8> {
62        vec![(*self) as u8]
63    }
64    fn to_bytes_as_i8(&self) -> Vec<u8> {
65        (*self as i8).to_le_bytes().to_vec()
66    }
67    fn to_bytes_as_u8(&self) -> Vec<u8> {
68        (*self as u8).to_le_bytes().to_vec()
69    }
70    fn to_bytes_as_i16(&self) -> Vec<u8> {
71        (*self as i16).to_le_bytes().to_vec()
72    }
73    fn to_bytes_as_u16(&self) -> Vec<u8> {
74        (*self as u16).to_le_bytes().to_vec()
75    }
76    fn to_bytes_as_i32(&self) -> Vec<u8> {
77        (*self as i32).to_le_bytes().to_vec()
78    }
79    fn to_bytes_as_u32(&self) -> Vec<u8> {
80        (*self as u32).to_le_bytes().to_vec()
81    }
82    fn to_bytes_as_f32(&self) -> Vec<u8> {
83        let value: f32 = if *self { 1.0 } else { 0.0 };
84        value.to_le_bytes().to_vec()
85    }
86    fn to_bytes_as_i64(&self) -> Vec<u8> {
87        (*self as i64).to_le_bytes().to_vec()
88    }
89    fn to_bytes_as_u64(&self) -> Vec<u8> {
90        (*self as u64).to_le_bytes().to_vec()
91    }
92    fn to_bytes_as_f64(&self) -> Vec<u8> {
93        let value: f64 = if *self { 1.0 } else { 0.0 };
94        value.to_le_bytes().to_vec()
95    }
96    fn to_bytes_as_string(&self) -> Vec<u8> {
97        self.to_string().as_bytes().to_vec()
98    }
99}
100impl SetParameterValue for &str {
101    fn to_bytes_as_bool(&self) -> Vec<u8> {
102        vec![]
103    }
104    fn to_bytes_as_i8(&self) -> Vec<u8> {
105        vec![]
106    }
107    fn to_bytes_as_u8(&self) -> Vec<u8> {
108        vec![]
109    }
110    fn to_bytes_as_i16(&self) -> Vec<u8> {
111        vec![]
112    }
113    fn to_bytes_as_u16(&self) -> Vec<u8> {
114        vec![]
115    }
116    fn to_bytes_as_i32(&self) -> Vec<u8> {
117        vec![]
118    }
119    fn to_bytes_as_u32(&self) -> Vec<u8> {
120        vec![]
121    }
122    fn to_bytes_as_f32(&self) -> Vec<u8> {
123        vec![]
124    }
125    fn to_bytes_as_i64(&self) -> Vec<u8> {
126        vec![]
127    }
128    fn to_bytes_as_u64(&self) -> Vec<u8> {
129        vec![]
130    }
131    fn to_bytes_as_f64(&self) -> Vec<u8> {
132        vec![]
133    }
134    fn to_bytes_as_string(&self) -> Vec<u8> {
135        self.as_bytes().to_vec()
136    }
137}
138
139impl SetParameterValue for String {
140    fn to_bytes_as_bool(&self) -> Vec<u8> {
141        vec![]
142    }
143    fn to_bytes_as_i8(&self) -> Vec<u8> {
144        vec![]
145    }
146    fn to_bytes_as_u8(&self) -> Vec<u8> {
147        vec![]
148    }
149    fn to_bytes_as_i16(&self) -> Vec<u8> {
150        vec![]
151    }
152    fn to_bytes_as_u16(&self) -> Vec<u8> {
153        vec![]
154    }
155    fn to_bytes_as_i32(&self) -> Vec<u8> {
156        vec![]
157    }
158    fn to_bytes_as_u32(&self) -> Vec<u8> {
159        vec![]
160    }
161    fn to_bytes_as_f32(&self) -> Vec<u8> {
162        vec![]
163    }
164    fn to_bytes_as_i64(&self) -> Vec<u8> {
165        vec![]
166    }
167    fn to_bytes_as_u64(&self) -> Vec<u8> {
168        vec![]
169    }
170    fn to_bytes_as_f64(&self) -> Vec<u8> {
171        vec![]
172    }
173    fn to_bytes_as_string(&self) -> Vec<u8> {
174        self.as_bytes().to_vec()
175    }
176}
177
178// Generate implementations for standard numeric types
179impl_set_parameter_value!(i8);
180impl_set_parameter_value!(u8);
181impl_set_parameter_value!(i16);
182impl_set_parameter_value!(u16);
183impl_set_parameter_value!(i32);
184impl_set_parameter_value!(u32);
185impl_set_parameter_value!(i64);
186impl_set_parameter_value!(u64);
187impl_set_parameter_value!(f32);
188impl_set_parameter_value!(f64);
189
190// Vector
191macro_rules! impl_set_parameter_array {
192    ($t:ty) => {
193        impl<const N: usize> SetParameterValue for [$t; N] {
194            fn to_bytes_as_bool(&self) -> Vec<u8> {
195                // Pre-allocate enough space in the vector
196                let mut result = Vec::with_capacity(N);
197                // Extend the vector with the bytes of each element
198                for &val in self {
199                    result.push((val != Default::default()) as u8);
200                }
201                result
202            }
203            fn to_bytes_as_i8(&self) -> Vec<u8> {
204                // Pre-allocate enough space in the vector
205                let mut result = Vec::with_capacity(N);
206                // Extend the vector with the bytes of each element
207                for &val in self {
208                    result.extend_from_slice(&(val as i8).to_le_bytes());
209                }
210                result
211            }
212            fn to_bytes_as_u8(&self) -> Vec<u8> {
213                // Pre-allocate enough space in the vector
214                let mut result = Vec::with_capacity(N);
215                // Extend the vector with the bytes of each element
216                for &val in self {
217                    result.extend_from_slice(&(val as u8).to_le_bytes());
218                }
219                result
220            }
221            fn to_bytes_as_i16(&self) -> Vec<u8> {
222                // Pre-allocate enough space in the vector
223                let mut result = Vec::with_capacity(N * size_of::<i16>());
224                // Extend the vector with the bytes of each element
225                for &val in self {
226                    result.extend_from_slice(&(val as i16).to_le_bytes());
227                }
228                result
229            }
230            fn to_bytes_as_u16(&self) -> Vec<u8> {
231                // Pre-allocate enough space in the vector
232                let mut result = Vec::with_capacity(N * size_of::<u16>());
233                // Extend the vector with the bytes of each element
234                for &val in self {
235                    result.extend_from_slice(&(val as u16).to_le_bytes());
236                }
237                result
238            }
239            fn to_bytes_as_i32(&self) -> Vec<u8> {
240                // Pre-allocate enough space in the vector
241                let mut result = Vec::with_capacity(N * size_of::<i32>());
242                // Extend the vector with the bytes of each element
243                for &val in self {
244                    result.extend_from_slice(&(val as i32).to_le_bytes());
245                }
246                result
247            }
248            fn to_bytes_as_u32(&self) -> Vec<u8> {
249                // Pre-allocate enough space in the vector
250                let mut result = Vec::with_capacity(N * size_of::<u32>());
251                // Extend the vector with the bytes of each element
252                for &val in self {
253                    result.extend_from_slice(&(val as u32).to_le_bytes());
254                }
255                result
256            }
257            fn to_bytes_as_f32(&self) -> Vec<u8> {
258                // Pre-allocate enough space in the vector
259                let mut result = Vec::with_capacity(N * size_of::<f32>());
260                // Extend the vector with the bytes of each element
261                for &val in self {
262                    result.extend_from_slice(&(val as f32).to_le_bytes());
263                }
264                result
265            }
266            fn to_bytes_as_i64(&self) -> Vec<u8> {
267                // Pre-allocate enough space in the vector
268                let mut result = Vec::with_capacity(N * size_of::<i64>());
269                // Extend the vector with the bytes of each element
270                for &val in self {
271                    result.extend_from_slice(&(val as i64).to_le_bytes());
272                }
273                result
274            }
275            fn to_bytes_as_u64(&self) -> Vec<u8> {
276                // Pre-allocate enough space in the vector
277                let mut result = Vec::with_capacity(N * size_of::<u64>());
278                // Extend the vector with the bytes of each element
279                for &val in self {
280                    result.extend_from_slice(&(val as u64).to_le_bytes());
281                }
282                result
283            }
284            fn to_bytes_as_f64(&self) -> Vec<u8> {
285                // Pre-allocate enough space in the vector
286                let mut result = Vec::with_capacity(N * size_of::<f64>());
287                // Extend the vector with the bytes of each element
288                for &val in self {
289                    result.extend_from_slice(&(val as f64).to_le_bytes());
290                }
291                result
292            }
293            fn to_bytes_as_string(&self) -> Vec<u8> {
294                vec![]
295            }
296        }
297    };
298}
299// Generate implementations for standard numeric types
300impl_set_parameter_array!(i8);
301impl_set_parameter_array!(u8);
302impl_set_parameter_array!(i16);
303impl_set_parameter_array!(u16);
304impl_set_parameter_array!(i32);
305impl_set_parameter_array!(u32);
306impl_set_parameter_array!(i64);
307impl_set_parameter_array!(u64);
308impl_set_parameter_array!(f32);
309impl_set_parameter_array!(f64);
310
311impl<const N: usize> SetParameterValue for [bool; N] {
312    fn to_bytes_as_bool(&self) -> Vec<u8> {
313        // Pre-allocate enough space in the vector
314        let mut result = Vec::with_capacity(N);
315        // Extend the vector with the bytes of each element
316        for &val in self {
317            result.push((val != Default::default()) as u8);
318        }
319        result
320    }
321    fn to_bytes_as_i8(&self) -> Vec<u8> {
322        // Pre-allocate enough space in the vector
323        let mut result = Vec::with_capacity(N);
324        // Extend the vector with the bytes of each element
325        for &val in self {
326            result.extend_from_slice(&(val as i8).to_le_bytes());
327        }
328        result
329    }
330    fn to_bytes_as_u8(&self) -> Vec<u8> {
331        // Pre-allocate enough space in the vector
332        let mut result = Vec::with_capacity(N);
333        // Extend the vector with the bytes of each element
334        for &val in self {
335            result.extend_from_slice(&(val as u8).to_le_bytes());
336        }
337        result
338    }
339    fn to_bytes_as_i16(&self) -> Vec<u8> {
340        // Pre-allocate enough space in the vector
341        let mut result = Vec::with_capacity(N * size_of::<i16>());
342        // Extend the vector with the bytes of each element
343        for &val in self {
344            result.extend_from_slice(&(val as i16).to_le_bytes());
345        }
346        result
347    }
348    fn to_bytes_as_u16(&self) -> Vec<u8> {
349        // Pre-allocate enough space in the vector
350        let mut result = Vec::with_capacity(N * size_of::<u16>());
351        // Extend the vector with the bytes of each element
352        for &val in self {
353            result.extend_from_slice(&(val as u16).to_le_bytes());
354        }
355        result
356    }
357    fn to_bytes_as_i32(&self) -> Vec<u8> {
358        // Pre-allocate enough space in the vector
359        let mut result = Vec::with_capacity(N * size_of::<i32>());
360        // Extend the vector with the bytes of each element
361        for &val in self {
362            result.extend_from_slice(&(val as i32).to_le_bytes());
363        }
364        result
365    }
366    fn to_bytes_as_u32(&self) -> Vec<u8> {
367        // Pre-allocate enough space in the vector
368        let mut result = Vec::with_capacity(N * size_of::<u32>());
369        // Extend the vector with the bytes of each element
370        for &val in self {
371            result.extend_from_slice(&(val as u32).to_le_bytes());
372        }
373        result
374    }
375    fn to_bytes_as_f32(&self) -> Vec<u8> {
376        // Pre-allocate enough space in the vector
377        let mut result = Vec::with_capacity(N * size_of::<f32>());
378        // Extend the vector with the bytes of each element
379        for &val in self {
380            result.extend_from_slice(&(val as u32 as f32).to_le_bytes());
381        }
382        result
383    }
384    fn to_bytes_as_i64(&self) -> Vec<u8> {
385        // Pre-allocate enough space in the vector
386        let mut result = Vec::with_capacity(N * size_of::<i64>());
387        // Extend the vector with the bytes of each element
388        for &val in self {
389            result.extend_from_slice(&(val as i64).to_le_bytes());
390        }
391        result
392    }
393    fn to_bytes_as_u64(&self) -> Vec<u8> {
394        // Pre-allocate enough space in the vector
395        let mut result = Vec::with_capacity(N * size_of::<u64>());
396        // Extend the vector with the bytes of each element
397        for &val in self {
398            result.extend_from_slice(&(val as u64).to_le_bytes());
399        }
400        result
401    }
402    fn to_bytes_as_f64(&self) -> Vec<u8> {
403        // Pre-allocate enough space in the vector
404        let mut result = Vec::with_capacity(N * size_of::<f64>());
405        // Extend the vector with the bytes of each element
406        for &val in self {
407            result.extend_from_slice(&(val as u64 as f64).to_le_bytes());
408        }
409        result
410    }
411    fn to_bytes_as_string(&self) -> Vec<u8> {
412        vec![]
413    }
414}
415
416macro_rules! impl_set_parameter_vec {
417    ($t:ty) => {
418        impl SetParameterValue for Vec<$t> {
419            fn to_bytes_as_bool(&self) -> Vec<u8> {
420                // Pre-allocate enough space in the vector
421                let mut result = Vec::with_capacity(self.len());
422                // Extend the vector with the bytes of each element
423                for &val in self {
424                    result.push((val != Default::default()) as u8);
425                }
426                result
427            }
428            fn to_bytes_as_i8(&self) -> Vec<u8> {
429                // Pre-allocate enough space in the vector
430                let mut result = Vec::with_capacity(self.len());
431                // Extend the vector with the bytes of each element
432                for &val in self {
433                    result.extend_from_slice(&(val as i8).to_le_bytes());
434                }
435                result
436            }
437            fn to_bytes_as_u8(&self) -> Vec<u8> {
438                // Pre-allocate enough space in the vector
439                let mut result = Vec::with_capacity(self.len());
440                // Extend the vector with the bytes of each element
441                for &val in self {
442                    result.extend_from_slice(&(val as u8).to_le_bytes());
443                }
444                result
445            }
446            fn to_bytes_as_i16(&self) -> Vec<u8> {
447                // Pre-allocate enough space in the vector
448                let mut result = Vec::with_capacity(self.len() * 2);
449                // Extend the vector with the bytes of each element
450                for &val in self {
451                    result.extend_from_slice(&(val as i16).to_le_bytes());
452                }
453                result
454            }
455            fn to_bytes_as_u16(&self) -> Vec<u8> {
456                // Pre-allocate enough space in the vector
457                let mut result = Vec::with_capacity(self.len() * 2);
458                // Extend the vector with the bytes of each element
459                for &val in self {
460                    result.extend_from_slice(&(val as u16).to_le_bytes());
461                }
462                result
463            }
464            fn to_bytes_as_i32(&self) -> Vec<u8> {
465                // Pre-allocate enough space in the vector
466                let mut result = Vec::with_capacity(self.len() * 4);
467                // Extend the vector with the bytes of each element
468                for &val in self {
469                    result.extend_from_slice(&(val as i32).to_le_bytes());
470                }
471                result
472            }
473            fn to_bytes_as_u32(&self) -> Vec<u8> {
474                // Pre-allocate enough space in the vector
475                let mut result = Vec::with_capacity(self.len() * 4);
476                // Extend the vector with the bytes of each element
477                for &val in self {
478                    result.extend_from_slice(&(val as u32).to_le_bytes());
479                }
480                result
481            }
482            fn to_bytes_as_f32(&self) -> Vec<u8> {
483                // Pre-allocate enough space in the vector
484                let mut result = Vec::with_capacity(self.len() * 4);
485                // Extend the vector with the bytes of each element
486                for &val in self {
487                    result.extend_from_slice(&(val as f32).to_le_bytes());
488                }
489                result
490            }
491            fn to_bytes_as_i64(&self) -> Vec<u8> {
492                // Pre-allocate enough space in the vector
493                let mut result = Vec::with_capacity(self.len() * 8);
494                // Extend the vector with the bytes of each element
495                for &val in self {
496                    result.extend_from_slice(&(val as i64).to_le_bytes());
497                }
498                result
499            }
500            fn to_bytes_as_u64(&self) -> Vec<u8> {
501                // Pre-allocate enough space in the vector
502                let mut result = Vec::with_capacity(self.len() * 8);
503                // Extend the vector with the bytes of each element
504                for &val in self {
505                    result.extend_from_slice(&(val as u64).to_le_bytes());
506                }
507                result
508            }
509            fn to_bytes_as_f64(&self) -> Vec<u8> {
510                // Pre-allocate enough space in the vector
511                let mut result = Vec::with_capacity(self.len() * 8);
512                // Extend the vector with the bytes of each element
513                for &val in self {
514                    result.extend_from_slice(&(val as f64).to_le_bytes());
515                }
516                result
517            }
518            fn to_bytes_as_string(&self) -> Vec<u8> {
519                vec![]
520            }
521        }
522    };
523}
524
525impl_set_parameter_vec!(i8);
526impl_set_parameter_vec!(u8);
527impl_set_parameter_vec!(i16);
528impl_set_parameter_vec!(u16);
529impl_set_parameter_vec!(i32);
530impl_set_parameter_vec!(u32);
531impl_set_parameter_vec!(i64);
532impl_set_parameter_vec!(u64);
533impl_set_parameter_vec!(f32);
534impl_set_parameter_vec!(f64);
535
536impl SetParameterValue for Vec<bool> {
537    fn to_bytes_as_bool(&self) -> Vec<u8> {
538        // Pre-allocate enough space in the vector
539        let mut result = Vec::with_capacity(self.len());
540        // Extend the vector with the bytes of each element
541        for &val in self {
542            result.push((val != Default::default()) as u8);
543        }
544        result
545    }
546    fn to_bytes_as_i8(&self) -> Vec<u8> {
547        // Pre-allocate enough space in the vector
548        let mut result = Vec::with_capacity(self.len());
549        // Extend the vector with the bytes of each element
550        for &val in self {
551            result.extend_from_slice(&(val as i8).to_le_bytes());
552        }
553        result
554    }
555    fn to_bytes_as_u8(&self) -> Vec<u8> {
556        // Pre-allocate enough space in the vector
557        let mut result = Vec::with_capacity(self.len());
558        // Extend the vector with the bytes of each element
559        for &val in self {
560            result.extend_from_slice(&(val as u8).to_le_bytes());
561        }
562        result
563    }
564    fn to_bytes_as_i16(&self) -> Vec<u8> {
565        // Pre-allocate enough space in the vector
566        let mut result = Vec::with_capacity(self.len() / 2);
567        // Extend the vector with the bytes of each element
568        for &val in self {
569            result.extend_from_slice(&(val as i16).to_le_bytes());
570        }
571        result
572    }
573    fn to_bytes_as_u16(&self) -> Vec<u8> {
574        // Pre-allocate enough space in the vector
575        let mut result = Vec::with_capacity(self.len() / 2);
576        // Extend the vector with the bytes of each element
577        for &val in self {
578            result.extend_from_slice(&(val as u16).to_le_bytes());
579        }
580        result
581    }
582    fn to_bytes_as_i32(&self) -> Vec<u8> {
583        // Pre-allocate enough space in the vector
584        let mut result = Vec::with_capacity(self.len() / 4);
585        // Extend the vector with the bytes of each element
586        for &val in self {
587            result.extend_from_slice(&(val as i32).to_le_bytes());
588        }
589        result
590    }
591    fn to_bytes_as_u32(&self) -> Vec<u8> {
592        // Pre-allocate enough space in the vector
593        let mut result = Vec::with_capacity(self.len() / 4);
594        // Extend the vector with the bytes of each element
595        for &val in self {
596            result.extend_from_slice(&(val as u32).to_le_bytes());
597        }
598        result
599    }
600    fn to_bytes_as_f32(&self) -> Vec<u8> {
601        // Pre-allocate enough space in the vector
602        let mut result = Vec::with_capacity(self.len() / 4);
603        // Extend the vector with the bytes of each element
604        for &val in self {
605            result.extend_from_slice(&(val as u32 as f32).to_le_bytes());
606        }
607        result
608    }
609    fn to_bytes_as_i64(&self) -> Vec<u8> {
610        // Pre-allocate enough space in the vector
611        let mut result = Vec::with_capacity(self.len() / 8);
612        // Extend the vector with the bytes of each element
613        for &val in self {
614            result.extend_from_slice(&(val as i64).to_le_bytes());
615        }
616        result
617    }
618    fn to_bytes_as_u64(&self) -> Vec<u8> {
619        // Pre-allocate enough space in the vector
620        let mut result = Vec::with_capacity(self.len() / 8);
621        // Extend the vector with the bytes of each element
622        for &val in self {
623            result.extend_from_slice(&(val as u64).to_le_bytes());
624        }
625        result
626    }
627    fn to_bytes_as_f64(&self) -> Vec<u8> {
628        // Pre-allocate enough space in the vector
629        let mut result = Vec::with_capacity(self.len() / 8);
630        // Extend the vector with the bytes of each element
631        for &val in self {
632            result.extend_from_slice(&(val as u64 as f64).to_le_bytes());
633        }
634        result
635    }
636    fn to_bytes_as_string(&self) -> Vec<u8> {
637        vec![]
638    }
639}