Skip to content

Commit b635d60

Browse files
committed
Move most uses of the std crate to core and alloc
std::io and std::error::Error are the two remaining uses of std preventing this crate from building in a #![no_std] context. I’ve tried reimplementing just these parts of std in this crate, but it was obviously not a good way forward, and I don’t know what could be a way forward here. In the meantime, these changes don’t cause any issue, and will be useful for the next person trying to implement no_std! f32::ceil() and f32::fract() are two other functions we use which are available only in std, I’m not too sure why.
1 parent ab6d326 commit b635d60

File tree

9 files changed

+47
-22
lines changed

9 files changed

+47
-22
lines changed

src/decoder.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
use alloc::borrow::ToOwned;
2+
use alloc::{format, vec};
3+
use alloc::vec::Vec;
4+
use alloc::sync::Arc;
5+
use core::cmp;
6+
use core::mem;
7+
use core::ops::Range;
18
use crate::read_u8;
29
use error::{Error, Result, UnsupportedFeature};
310
use huffman::{fill_default_mjpeg_tables, HuffmanDecoder, HuffmanTable};
@@ -6,11 +13,7 @@ use parser::{AdobeColorTransform, AppData, CodingProcess, Component, Dimensions,
613
parse_app, parse_com, parse_dht, parse_dqt, parse_dri, parse_sof, parse_sos, IccChunk,
714
ScanInfo};
815
use upsampler::Upsampler;
9-
use std::cmp;
1016
use std::io::Read;
11-
use std::mem;
12-
use std::ops::Range;
13-
use std::sync::Arc;
1417
use worker::{RowData, PlatformWorker, Worker};
1518

1619
pub const MAX_COMPONENTS: usize = 4;
@@ -1090,6 +1093,6 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
10901093
}
10911094

10921095
fn clamp_to_u8(value: i32) -> i32 {
1093-
let value = std::cmp::max(value, 0);
1094-
std::cmp::min(value, 255)
1096+
let value = cmp::max(value, 0);
1097+
cmp::min(value, 255)
10951098
}

src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use alloc::boxed::Box;
2+
use alloc::string::String;
3+
use alloc::fmt;
4+
use core::result;
15
use std::error::Error as StdError;
2-
use std::fmt;
36
use std::io::Error as IoError;
47

5-
pub type Result<T> = ::std::result::Result<T, Error>;
8+
pub type Result<T> = result::Result<T, Error>;
69

710
/// An enumeration over JPEG features (currently) unsupported by this library.
811
///

src/huffman.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use alloc::borrow::ToOwned;
2+
use alloc::vec;
3+
use alloc::vec::Vec;
4+
use core::iter;
15
use crate::read_u8;
26
use error::{Error, Result};
37
use marker::Marker;
@@ -253,7 +257,7 @@ fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
253257
let huffsize = bits.iter()
254258
.enumerate()
255259
.fold(Vec::new(), |mut acc, (i, &value)| {
256-
acc.extend(std::iter::repeat((i + 1) as u8).take(value as usize));
260+
acc.extend(iter::repeat((i + 1) as u8).take(value as usize));
257261
acc
258262
});
259263

src/idct.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// One example is tests/crashtest/images/imagetestsuite/b0b8914cc5f7a6eff409f16d8cc236c5.jpg
33
// That's why wrapping operators are needed.
44
use crate::parser::Dimensions;
5-
use std::{convert::TryFrom, num::Wrapping};
5+
use core::{convert::TryFrom, num::Wrapping};
66

77
pub(crate) fn choose_idct_size(full_size: Dimensions, requested_size: Dimensions) -> usize {
88
fn scaled(len: u16, scale: usize) -> u16 { ((len as u32 * scale as u32 - 1) / 8 + 1) as u16 }
@@ -417,8 +417,8 @@ fn test_dequantize_and_idct_block_8x8_all_zero() {
417417
fn test_dequantize_and_idct_block_8x8_saturated() {
418418
let mut output = [0u8; 8 * 8];
419419
dequantize_and_idct_block_8x8(
420-
&[std::i16::MAX; 8*8],
421-
&[std::u16::MAX; 8*8],
420+
&[i16::MAX; 8*8],
421+
&[u16::MAX; 8*8],
422422
8,
423423
&mut output);
424424
let expected = [

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
#![deny(missing_docs)]
3030
#![forbid(unsafe_code)]
3131

32+
extern crate core;
33+
extern crate alloc;
34+
3235
#[cfg(feature="rayon")]
3336
extern crate rayon;
3437

3538
pub use decoder::{Decoder, ImageInfo, PixelFormat};
3639
pub use error::{Error, UnsupportedFeature};
3740

41+
use std::io;
42+
3843
mod decoder;
3944
mod error;
4045
mod huffman;
@@ -44,13 +49,13 @@ mod parser;
4449
mod upsampler;
4550
mod worker;
4651

47-
fn read_u8<R: std::io::Read>(reader: &mut R) -> std::io::Result<u8> {
52+
fn read_u8<R: io::Read>(reader: &mut R) -> io::Result<u8> {
4853
let mut buf = [0];
4954
reader.read_exact(&mut buf)?;
5055
Ok(buf[0])
5156
}
5257

53-
fn read_u16_from_be<R: std::io::Read>(reader: &mut R) -> std::io::Result<u16> {
58+
fn read_u16_from_be<R: io::Read>(reader: &mut R) -> io::Result<u16> {
5459
let mut buf = [0, 0];
5560
reader.read_exact(&mut buf)?;
5661
Ok(u16::from_be_bytes(buf))

src/parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use alloc::borrow::ToOwned;
2+
use alloc::{format, vec};
3+
use alloc::vec::Vec;
4+
use core::ops::{self, Range};
15
use crate::{read_u16_from_be, read_u8};
26
use error::{Error, Result, UnsupportedFeature};
37
use huffman::{HuffmanTable, HuffmanTableClass};
48
use marker::Marker;
59
use marker::Marker::*;
610
use std::io::{self, Read};
7-
use std::ops::Range;
811

912
#[derive(Clone, Copy, Debug, PartialEq)]
1013
pub struct Dimensions {
@@ -359,7 +362,7 @@ pub fn parse_sos<R: Read>(reader: &mut R, frame: &FrameInfo) -> Result<ScanInfo>
359362

360363
let blocks_per_mcu = component_indices.iter().map(|&i| {
361364
frame.components[i].horizontal_sampling_factor as u32 * frame.components[i].vertical_sampling_factor as u32
362-
}).fold(0, ::std::ops::Add::add);
365+
}).fold(0, ops::Add::add);
363366

364367
if component_count > 1 && blocks_per_mcu > 10 {
365368
return Err(Error::Format("scan with more than one component and more than 10 blocks per MCU".to_owned()));
@@ -489,7 +492,7 @@ pub fn parse_dht<R: Read>(reader: &mut R, is_baseline: Option<bool>) -> Result<(
489492
let mut counts = [0u8; 16];
490493
reader.read_exact(&mut counts)?;
491494

492-
let size = counts.iter().map(|&val| val as usize).fold(0, ::std::ops::Add::add);
495+
let size = counts.iter().map(|&val| val as usize).fold(0, ops::Add::add);
493496

494497
if size == 0 {
495498
return Err(Error::Format("encountered table with zero length in DHT".to_owned()));

src/upsampler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use alloc::boxed::Box;
2+
use alloc::vec;
3+
use alloc::vec::Vec;
14
use error::{Error, Result, UnsupportedFeature};
25
use parser::Component;
36

src/worker/immediate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use alloc::vec;
2+
use alloc::vec::Vec;
3+
use core::mem;
14
use decoder::MAX_COMPONENTS;
25
use error::Result;
36
use idct::dequantize_and_idct_block;
4-
use std::mem;
5-
use std::sync::Arc;
7+
use alloc::sync::Arc;
68
use parser::Component;
79
use super::{RowData, Worker};
810

src/worker/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
mod immediate;
2+
#[cfg(feature = "std")]
23
mod multithreaded;
34

4-
#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))]
5+
#[cfg(all(feature = "std", not(any(target_arch = "asmjs", target_arch = "wasm32"))))]
56
pub use self::multithreaded::MultiThreadedWorker as PlatformWorker;
6-
#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))]
7+
#[cfg(any(not(feature = "std"), target_arch = "asmjs", target_arch = "wasm32"))]
78
pub use self::immediate::ImmediateWorker as PlatformWorker;
89

10+
use alloc::sync::Arc;
11+
use alloc::vec::Vec;
912
use error::Result;
1013
use parser::Component;
11-
use std::sync::Arc;
1214

1315
pub struct RowData {
1416
pub index: usize,

0 commit comments

Comments
 (0)