@@ -26,71 +26,79 @@ unsafe fn convert_ptr_mut<'r, T>(r: *mut u8) -> &'r mut T {
26
26
unsafe { & mut * ( r. cast :: < T > ( ) ) }
27
27
}
28
28
29
- #[ inline]
30
- fn match_format < T : DataType > ( mat_type : i32 ) -> Result < ( ) > {
31
- let out_type = T :: opencv_type ( ) ;
32
- if mat_type == out_type {
33
- Ok ( ( ) )
34
- } else {
35
- let mat_type = core:: type_to_string ( mat_type) ?;
36
- let out_type = core:: type_to_string ( out_type) ?;
37
- Err ( Error :: new (
38
- core:: StsUnmatchedFormats ,
39
- format ! ( "Mat type is: {mat_type}, but requested type is: {out_type}" ) ,
40
- ) )
41
- }
29
+ trait MatMatcher {
30
+ fn match_indices ( & self , idx : & [ i32 ] ) -> Result < ( ) > ;
31
+ fn match_total ( & self , idx : i32 ) -> Result < ( ) > ;
32
+ fn match_is_continuous ( & self ) -> Result < ( ) > ;
42
33
}
43
34
44
- fn match_indices ( mat : & ( impl MatTraitConst + ?Sized ) , idx : & [ i32 ] ) -> Result < ( ) > {
45
- let mat_size = mat. mat_size ( ) ;
46
- let size = & * mat_size;
47
- if size. len ( ) != idx. len ( ) {
48
- return Err ( Error :: new (
49
- core:: StsUnmatchedSizes ,
50
- format ! (
51
- "Amount of Mat dimensions: {} doesn't match the amount of requested indices: {}" ,
52
- size. len( ) ,
53
- idx. len( )
54
- ) ,
55
- ) ) ;
56
- }
57
- if let Some ( ( out_idx, ( out_idx_val, out_size) ) ) = idx
58
- . iter ( )
59
- . zip ( size)
60
- . enumerate ( )
61
- . find ( |( _, ( idx_val, & size) ) | !( 0 ..size) . contains ( idx_val) )
62
- {
63
- Err ( Error :: new (
64
- core:: StsOutOfRange ,
65
- format ! ( "Index: {out_idx_val} along dimension: {out_idx} out of bounds 0..{out_size}" ) ,
66
- ) )
67
- } else {
68
- Ok ( ( ) )
35
+ impl < T : MatTraitConst + ?Sized > MatMatcher for T {
36
+ fn match_indices ( & self , idx : & [ i32 ] ) -> Result < ( ) > {
37
+ let mat_size = self . mat_size ( ) ;
38
+ let size = & * mat_size;
39
+ if size. len ( ) != idx. len ( ) {
40
+ return Err ( Error :: new (
41
+ core:: StsUnmatchedSizes ,
42
+ format ! (
43
+ "Amount of Mat dimensions: {} doesn't match the amount of requested indices: {}" ,
44
+ size. len( ) ,
45
+ idx. len( )
46
+ ) ,
47
+ ) ) ;
48
+ }
49
+ if let Some ( ( out_idx, ( out_idx_val, out_size) ) ) = idx
50
+ . iter ( )
51
+ . zip ( size)
52
+ . enumerate ( )
53
+ . find ( |( _, ( idx_val, & size) ) | !( 0 ..size) . contains ( idx_val) )
54
+ {
55
+ Err ( Error :: new (
56
+ core:: StsOutOfRange ,
57
+ format ! ( "Index: {out_idx_val} along dimension: {out_idx} out of bounds 0..{out_size}" ) ,
58
+ ) )
59
+ } else {
60
+ Ok ( ( ) )
61
+ }
69
62
}
70
- }
71
63
72
- #[ inline]
73
- fn match_total ( mat : & ( impl MatTraitConst + ?Sized ) , idx : i32 ) -> Result < ( ) > {
74
- let size = mat. total ( ) ;
75
- // safe because of the `0 <= idx` check
76
- if 0 <= idx && ( idx as usize ) < size {
77
- Ok ( ( ) )
78
- } else {
79
- Err ( Error :: new (
80
- core:: StsOutOfRange ,
81
- format ! ( "Index: {idx} out of bounds: 0..{size}" ) ,
82
- ) )
64
+ #[ inline]
65
+ fn match_total ( & self , idx : i32 ) -> Result < ( ) > {
66
+ let size = self . total ( ) ;
67
+ // safe because of the `0 <= idx` check
68
+ if 0 <= idx && ( idx as usize ) < size {
69
+ Ok ( ( ) )
70
+ } else {
71
+ Err ( Error :: new (
72
+ core:: StsOutOfRange ,
73
+ format ! ( "Index: {idx} out of bounds: 0..{size}" ) ,
74
+ ) )
75
+ }
76
+ }
77
+
78
+ #[ inline]
79
+ fn match_is_continuous ( & self ) -> Result < ( ) > {
80
+ if self . is_continuous ( ) {
81
+ Ok ( ( ) )
82
+ } else {
83
+ Err ( Error :: new (
84
+ core:: StsUnmatchedSizes ,
85
+ "Mat is not continuous, operation is not applicable" ,
86
+ ) )
87
+ }
83
88
}
84
89
}
85
90
86
91
#[ inline]
87
- fn match_is_continuous ( mat : & ( impl MatTraitConst + ?Sized ) ) -> Result < ( ) > {
88
- if mat. is_continuous ( ) {
92
+ fn match_format < T : DataType > ( mat_type : i32 ) -> Result < ( ) > {
93
+ let out_type = T :: opencv_type ( ) ;
94
+ if mat_type == out_type {
89
95
Ok ( ( ) )
90
96
} else {
97
+ let mat_type = core:: type_to_string ( mat_type) ?;
98
+ let out_type = core:: type_to_string ( out_type) ?;
91
99
Err ( Error :: new (
92
- core:: StsUnmatchedSizes ,
93
- "Mat is not continuous, operation is not applicable" ,
100
+ core:: StsUnmatchedFormats ,
101
+ format ! ( "Mat type is: {mat_type}, but requested type is: {out_type}" ) ,
94
102
) )
95
103
}
96
104
}
@@ -403,7 +411,7 @@ pub(crate) mod mat_forward {
403
411
#[ inline]
404
412
pub fn at < T : DataType > ( mat : & ( impl MatTraitConst + ?Sized ) , i0 : i32 ) -> Result < & T > {
405
413
match_format :: < T > ( mat. typ ( ) )
406
- . and_then ( |_| match_total ( mat , i0) )
414
+ . and_then ( |_| mat . match_total ( i0) )
407
415
. and_then ( |_| unsafe { mat. at_unchecked ( i0) } )
408
416
}
409
417
@@ -414,7 +422,7 @@ pub(crate) mod mat_forward {
414
422
415
423
#[ inline]
416
424
pub fn at_mut < T : DataType > ( mat : & mut ( impl MatTrait + ?Sized ) , i0 : i32 ) -> Result < & mut T > {
417
- match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| match_total ( mat , i0) ) ?;
425
+ match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| mat . match_total ( i0) ) ?;
418
426
unsafe { mat. at_unchecked_mut ( i0) }
419
427
}
420
428
@@ -426,13 +434,13 @@ pub(crate) mod mat_forward {
426
434
#[ inline]
427
435
pub fn at_2d < T : DataType > ( mat : & ( impl MatTraitConst + ?Sized ) , row : i32 , col : i32 ) -> Result < & T > {
428
436
match_format :: < T > ( mat. typ ( ) )
429
- . and_then ( |_| match_indices ( mat , & [ row, col] ) )
437
+ . and_then ( |_| mat . match_indices ( & [ row, col] ) )
430
438
. and_then ( |_| unsafe { mat. at_2d_unchecked ( row, col) } )
431
439
}
432
440
433
441
#[ inline]
434
442
pub fn at_2d_mut < T : DataType > ( mat : & mut ( impl MatTrait + ?Sized ) , row : i32 , col : i32 ) -> Result < & mut T > {
435
- match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| match_indices ( mat , & [ row, col] ) ) ?;
443
+ match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| mat . match_indices ( & [ row, col] ) ) ?;
436
444
unsafe { mat. at_2d_unchecked_mut ( row, col) }
437
445
}
438
446
@@ -449,26 +457,26 @@ pub(crate) mod mat_forward {
449
457
#[ inline]
450
458
pub fn at_3d < T : DataType > ( mat : & ( impl MatTraitConst + ?Sized ) , i0 : i32 , i1 : i32 , i2 : i32 ) -> Result < & T > {
451
459
match_format :: < T > ( mat. typ ( ) )
452
- . and_then ( |_| match_indices ( mat , & [ i0, i1, i2] ) )
460
+ . and_then ( |_| mat . match_indices ( & [ i0, i1, i2] ) )
453
461
. and_then ( |_| unsafe { mat. at_3d_unchecked ( i0, i1, i2) } )
454
462
}
455
463
456
464
#[ inline]
457
465
pub fn at_3d_mut < T : DataType > ( mat : & mut ( impl MatTrait + ?Sized ) , i0 : i32 , i1 : i32 , i2 : i32 ) -> Result < & mut T > {
458
- match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| match_indices ( mat , & [ i0, i1, i2] ) ) ?;
466
+ match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| mat . match_indices ( & [ i0, i1, i2] ) ) ?;
459
467
unsafe { mat. at_3d_unchecked_mut ( i0, i1, i2) }
460
468
}
461
469
462
470
#[ inline]
463
471
pub fn at_nd < ' s , T : DataType > ( mat : & ' s ( impl MatTraitConst + ?Sized ) , idx : & [ i32 ] ) -> Result < & ' s T > {
464
472
match_format :: < T > ( mat. typ ( ) )
465
- . and_then ( |_| match_indices ( mat , idx) )
473
+ . and_then ( |_| mat . match_indices ( idx) )
466
474
. and_then ( |_| unsafe { mat. at_nd_unchecked ( idx) } )
467
475
}
468
476
469
477
#[ inline]
470
478
pub fn at_nd_mut < ' s , T : DataType > ( mat : & ' s mut ( impl MatTrait + ?Sized ) , idx : & [ i32 ] ) -> Result < & ' s mut T > {
471
- match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| match_indices ( mat , idx) ) ?;
479
+ match_format :: < T > ( mat. typ ( ) ) . and_then ( |_| mat . match_indices ( idx) ) ?;
472
480
unsafe { mat. at_nd_unchecked_mut ( idx) }
473
481
}
474
482
}
@@ -519,7 +527,7 @@ pub trait MatTraitConstManual: MatTraitConst {
519
527
#[ inline]
520
528
fn at_row < T : DataType > ( & self , row : i32 ) -> Result < & [ T ] > {
521
529
match_format :: < T > ( self . typ ( ) )
522
- . and_then ( |_| match_indices ( self , & [ row, 0 ] ) )
530
+ . and_then ( |_| self . match_indices ( & [ row, 0 ] ) )
523
531
. and_then ( |_| unsafe { self . at_row_unchecked ( row) } )
524
532
}
525
533
@@ -547,7 +555,7 @@ pub trait MatTraitConstManual: MatTraitConst {
547
555
/// Returns the underlying data array as a byte slice, [Mat] must be continuous
548
556
#[ inline]
549
557
fn data_bytes ( & self ) -> Result < & [ u8 ] > {
550
- match_is_continuous ( self ) . and_then ( |_| {
558
+ self . match_is_continuous ( ) . and_then ( |_| {
551
559
let data = self . data ( ) ;
552
560
Ok ( if data. is_null ( ) {
553
561
& [ ]
@@ -560,7 +568,7 @@ pub trait MatTraitConstManual: MatTraitConst {
560
568
#[ inline]
561
569
fn data_typed < T : DataType > ( & self ) -> Result < & [ T ] > {
562
570
match_format :: < T > ( self . typ ( ) )
563
- . and_then ( |_| match_is_continuous ( self ) )
571
+ . and_then ( |_| self . match_is_continuous ( ) )
564
572
. and_then ( |_| unsafe { self . data_typed_unchecked ( ) } )
565
573
}
566
574
@@ -686,7 +694,7 @@ pub trait MatTraitManual: MatTraitConstManual + MatTrait {
686
694
/// Return a complete writeable row
687
695
#[ inline]
688
696
fn at_row_mut < T : DataType > ( & mut self , row : i32 ) -> Result < & mut [ T ] > {
689
- match_format :: < T > ( self . typ ( ) ) . and_then ( |_| match_indices ( self , & [ row, 0 ] ) ) ?;
697
+ match_format :: < T > ( self . typ ( ) ) . and_then ( |_| self . match_indices ( & [ row, 0 ] ) ) ?;
690
698
unsafe { self . at_row_unchecked_mut ( row) }
691
699
}
692
700
@@ -709,7 +717,7 @@ pub trait MatTraitManual: MatTraitConstManual + MatTrait {
709
717
/// Returns the underlying data array as a mutable byte slice, Mat must be continuous.
710
718
#[ inline]
711
719
fn data_bytes_mut ( & mut self ) -> Result < & mut [ u8 ] > {
712
- match_is_continuous ( self ) . and_then ( |_| {
720
+ self . match_is_continuous ( ) . and_then ( |_| {
713
721
let data = self . data_mut ( ) ;
714
722
Ok ( if data. is_null ( ) {
715
723
& mut [ ]
@@ -721,7 +729,7 @@ pub trait MatTraitManual: MatTraitConstManual + MatTrait {
721
729
722
730
#[ inline]
723
731
fn data_typed_mut < T : DataType > ( & mut self ) -> Result < & mut [ T ] > {
724
- match_format :: < T > ( self . typ ( ) ) . and_then ( |_| match_is_continuous ( self ) ) ?;
732
+ match_format :: < T > ( self . typ ( ) ) . and_then ( |_| self . match_is_continuous ( ) ) ?;
725
733
unsafe { self . data_typed_unchecked_mut ( ) }
726
734
}
727
735
0 commit comments