Skip to content

Commit 761cdd8

Browse files
committed
Add types to support fallible allocation for String, HashMap and Box
Resolves #204 Also, add dummy zero-sized types to all the non-test code to help avoid using the stdlib types by accident.
1 parent 921314e commit 761cdd8

File tree

6 files changed

+285
-142
lines changed

6 files changed

+285
-142
lines changed

mp4parse/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ travis-ci = { repository = "https://github.com/mozilla/mp4parse-rust" }
2626
[dependencies]
2727
byteorder = "1.2.1"
2828
bitreader = { version = "0.3.2" }
29+
hashbrown = "0.7.1"
2930
num-traits = "0.2.0"
3031
mp4parse_fallible = { version = "0.0.3", optional = true }
3132
log = "0.4"

mp4parse/src/boxes.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
use std::fmt;
55

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+
616
macro_rules! box_database {
717
($($boxenum:ident $boxtype:expr),*,) => {
818
#[derive(Clone, Copy, PartialEq)]
@@ -42,24 +52,14 @@ macro_rules! box_database {
4252

4353
#[derive(Default, PartialEq, Clone)]
4454
pub struct FourCC {
45-
pub value: String,
55+
pub value: [u8; 4],
4656
}
4757

4858
impl From<u32> for FourCC {
4959
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(),
5462
}
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 }
6363
}
6464
}
6565

@@ -70,23 +70,27 @@ impl From<BoxType> for FourCC {
7070
}
7171
}
7272

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 }
7876
}
7977
}
8078

8179
impl fmt::Debug for FourCC {
8280
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+
}
8485
}
8586
}
8687

8788
impl fmt::Display for FourCC {
8889
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+
}
9094
}
9195
}
9296

0 commit comments

Comments
 (0)