Skip to content

Commit b15262f

Browse files
authored
Merge pull request #196 from linkmauve/almost-no_std
Move most uses of the std crate to core and alloc
2 parents 432b417 + a96dcd2 commit b15262f

File tree

11 files changed

+51
-26
lines changed

11 files changed

+51
-26
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
rust: ["1.36.0", stable, beta, nightly]
15+
rust: ["1.48.0", stable, beta, nightly]
1616
features: ["", "rayon"]
1717
command: [test, benchmark]
1818

@@ -29,11 +29,11 @@ jobs:
2929
- name: test
3030
run: >
3131
cargo test --tests --benches --no-default-features --features "$FEATURES"
32-
if: ${{ matrix.command == 'test' && matrix.rust != '1.36.0' }}
32+
if: ${{ matrix.command == 'test' && matrix.rust != '1.48.0' }}
3333
env:
3434
FEATURES: ${{ matrix.features }}
3535
- name: benchmark
3636
run: cargo bench --bench decoding_benchmark --no-default-features --features "$FEATURES" -- --warm-up-time 1 --measurement-time 1 --sample-size 25
37-
if: ${{ matrix.command == 'benchmark' && matrix.rust != '1.36.0' }}
37+
if: ${{ matrix.command == 'benchmark' && matrix.rust != '1.48.0' }}
3838
env:
3939
FEATURES: ${{ matrix.features }}

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.36
1+
1.48.0

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;
@@ -1122,6 +1125,6 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
11221125
}
11231126

11241127
fn clamp_to_u8(value: i32) -> i32 {
1125-
let value = std::cmp::max(value, 0);
1126-
std::cmp::min(value, 255)
1128+
let value = cmp::max(value, 0);
1129+
cmp::min(value, 255)
11271130
}

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,13 +29,18 @@
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
pub use parser::CodingProcess;
3841

42+
use std::io;
43+
3944
mod decoder;
4045
mod error;
4146
mod huffman;
@@ -45,13 +50,13 @@ mod parser;
4550
mod upsampler;
4651
mod worker;
4752

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

54-
fn read_u16_from_be<R: std::io::Read>(reader: &mut R) -> std::io::Result<u16> {
59+
fn read_u16_from_be<R: io::Read>(reader: &mut R) -> io::Result<u16> {
5560
let mut buf = [0, 0];
5661
reader.read_exact(&mut buf)?;
5762
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 {
@@ -379,7 +382,7 @@ pub fn parse_sos<R: Read>(reader: &mut R, frame: &FrameInfo) -> Result<ScanInfo>
379382

380383
let blocks_per_mcu = component_indices.iter().map(|&i| {
381384
frame.components[i].horizontal_sampling_factor as u32 * frame.components[i].vertical_sampling_factor as u32
382-
}).fold(0, ::std::ops::Add::add);
385+
}).fold(0, ops::Add::add);
383386

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

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

542545
if size == 0 {
543546
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

0 commit comments

Comments
 (0)