Skip to content

Commit 74ac065

Browse files
authored
Merge pull request #213 from mozilla/try-string-hashmap-box
Add types to support fallible allocation for String, HashMap and Box
2 parents 921314e + 8c707c2 commit 74ac065

File tree

9 files changed

+680
-425
lines changed

9 files changed

+680
-425
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,3 @@ overflow-checks = true
1010

1111
[profile.bench]
1212
overflow-checks = true
13-
14-
# Uncomment below to test local changes to mp4parse_fallible crate
15-
# [patch.crates-io]
16-
# mp4parse_fallible = { path = "../mp4parse_fallible" }

mp4parse/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ 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"
30-
mp4parse_fallible = { version = "0.0.3", optional = true }
3131
log = "0.4"
3232
static_assertions = "1.1.0"
3333

3434
[dev-dependencies]
3535
test-assembler = "0.1.2"
3636
env_logger = "0.7.1"
37+
38+
[features]
39+
# Enable mp4parse_fallible to use fallible memory allocation rather than
40+
# panicking on OOM. Note that this is only safe within Gecko where the system
41+
# allocator has been globally overridden (see BMO 1457359).
42+
mp4parse_fallible = []

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)