3
3
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
4
use std:: fmt;
5
5
6
+ // To ensure we don't use stdlib allocating types by accident
7
+ #[ allow( dead_code) ]
8
+ struct Vec ;
9
+ #[ allow( dead_code) ]
10
+ struct Box ;
11
+ #[ allow( dead_code) ]
12
+ struct HashMap ;
13
+ #[ allow( dead_code) ]
14
+ struct String ;
15
+
6
16
macro_rules! box_database {
7
17
( $( $boxenum: ident $boxtype: expr) ,* , ) => {
8
18
#[ derive( Clone , Copy , PartialEq ) ]
@@ -42,24 +52,14 @@ macro_rules! box_database {
42
52
43
53
#[ derive( Default , PartialEq , Clone ) ]
44
54
pub struct FourCC {
45
- pub value : String ,
55
+ pub value : [ u8 ; 4 ] ,
46
56
}
47
57
48
58
impl From < u32 > for FourCC {
49
59
fn from ( number : u32 ) -> FourCC {
50
- let mut box_chars = Vec :: new ( ) ;
51
- for x in 0 ..4 {
52
- let c = ( number >> ( x * 8 ) & 0x0000_00FF ) as u8 ;
53
- box_chars. push ( c) ;
60
+ FourCC {
61
+ value : number. to_be_bytes ( ) ,
54
62
}
55
- box_chars. reverse ( ) ;
56
-
57
- let box_string = match String :: from_utf8 ( box_chars) {
58
- Ok ( t) => t,
59
- _ => String :: from ( "null" ) , // error to retrieve fourcc
60
- } ;
61
-
62
- FourCC { value : box_string }
63
63
}
64
64
}
65
65
@@ -70,23 +70,27 @@ impl From<BoxType> for FourCC {
70
70
}
71
71
}
72
72
73
- impl < ' a > From < & ' a str > for FourCC {
74
- fn from ( v : & ' a str ) -> FourCC {
75
- FourCC {
76
- value : v. to_owned ( ) ,
77
- }
73
+ impl From < [ u8 ; 4 ] > for FourCC {
74
+ fn from ( v : [ u8 ; 4 ] ) -> FourCC {
75
+ FourCC { value : v }
78
76
}
79
77
}
80
78
81
79
impl fmt:: Debug for FourCC {
82
80
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
83
- write ! ( f, "{}" , self . value)
81
+ match std:: str:: from_utf8 ( & self . value ) {
82
+ Ok ( s) => write ! ( f, "{}" , s) ,
83
+ Err ( _) => self . value . fmt ( f) ,
84
+ }
84
85
}
85
86
}
86
87
87
88
impl fmt:: Display for FourCC {
88
89
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
89
- write ! ( f, "{}" , self . value)
90
+ match std:: str:: from_utf8 ( & self . value ) {
91
+ Ok ( s) => write ! ( f, "{}" , s) ,
92
+ Err ( _) => write ! ( f, "null" ) ,
93
+ }
90
94
}
91
95
}
92
96
0 commit comments