Skip to content

Commit

Permalink
Merge pull request #213 from mozilla/try-string-hashmap-box
Browse files Browse the repository at this point in the history
Add types to support fallible allocation for String, HashMap and Box
  • Loading branch information
baumanj authored Mar 24, 2020
2 parents 921314e + 8c707c2 commit 74ac065
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 425 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ overflow-checks = true

[profile.bench]
overflow-checks = true

# Uncomment below to test local changes to mp4parse_fallible crate
# [patch.crates-io]
# mp4parse_fallible = { path = "../mp4parse_fallible" }
8 changes: 7 additions & 1 deletion mp4parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ travis-ci = { repository = "https://github.com/mozilla/mp4parse-rust" }
[dependencies]
byteorder = "1.2.1"
bitreader = { version = "0.3.2" }
hashbrown = "0.7.1"
num-traits = "0.2.0"
mp4parse_fallible = { version = "0.0.3", optional = true }
log = "0.4"
static_assertions = "1.1.0"

[dev-dependencies]
test-assembler = "0.1.2"
env_logger = "0.7.1"

[features]
# Enable mp4parse_fallible to use fallible memory allocation rather than
# panicking on OOM. Note that this is only safe within Gecko where the system
# allocator has been globally overridden (see BMO 1457359).
mp4parse_fallible = []
44 changes: 24 additions & 20 deletions mp4parse/src/boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use std::fmt;

// To ensure we don't use stdlib allocating types by accident
#[allow(dead_code)]
struct Vec;
#[allow(dead_code)]
struct Box;
#[allow(dead_code)]
struct HashMap;
#[allow(dead_code)]
struct String;

macro_rules! box_database {
($($boxenum:ident $boxtype:expr),*,) => {
#[derive(Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -42,24 +52,14 @@ macro_rules! box_database {

#[derive(Default, PartialEq, Clone)]
pub struct FourCC {
pub value: String,
pub value: [u8; 4],
}

impl From<u32> for FourCC {
fn from(number: u32) -> FourCC {
let mut box_chars = Vec::new();
for x in 0..4 {
let c = (number >> (x * 8) & 0x0000_00FF) as u8;
box_chars.push(c);
FourCC {
value: number.to_be_bytes(),
}
box_chars.reverse();

let box_string = match String::from_utf8(box_chars) {
Ok(t) => t,
_ => String::from("null"), // error to retrieve fourcc
};

FourCC { value: box_string }
}
}

Expand All @@ -70,23 +70,27 @@ impl From<BoxType> for FourCC {
}
}

impl<'a> From<&'a str> for FourCC {
fn from(v: &'a str) -> FourCC {
FourCC {
value: v.to_owned(),
}
impl From<[u8; 4]> for FourCC {
fn from(v: [u8; 4]) -> FourCC {
FourCC { value: v }
}
}

impl fmt::Debug for FourCC {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
match std::str::from_utf8(&self.value) {
Ok(s) => write!(f, "{}", s),
Err(_) => self.value.fmt(f),
}
}
}

impl fmt::Display for FourCC {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
match std::str::from_utf8(&self.value) {
Ok(s) => write!(f, "{}", s),
Err(_) => write!(f, "null"),
}
}
}

Expand Down
Loading

0 comments on commit 74ac065

Please sign in to comment.