Skip to content

Commit aed9e2e

Browse files
committed
Use built-in try_reserve
1 parent 5feb415 commit aed9e2e

File tree

10 files changed

+30
-29
lines changed

10 files changed

+30
-29
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ doctest = false
2626

2727
[dependencies]
2828
arrayvec = "0.7.2"
29-
fallible_collections = "0.4.4"
3029
noisy_float = "0.2.0"
3130
rgb = { version = "0.8.31", features = ["argb"] }
3231
rayon = { version = "1.5.1", optional = true }

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use fallible_collections::TryReserveError;
2-
pub use Error::*;
1+
use std::collections::TryReserveError;
32
use std::fmt;
3+
pub use Error::*;
44

55
/// Error codes
66
#[cfg_attr(feature = "_internal_c_ffi", repr(C))]

src/hist.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::error::*;
22
use crate::image::Image;
3-
use crate::pal::MAX_COLORS;
43
use crate::pal::PalIndex;
54
use crate::pal::ARGBF;
5+
use crate::pal::MAX_COLORS;
66
use crate::pal::{f_pixel, gamma_lut, RGBA};
77
use crate::quant::QuantizationResult;
88
use crate::rows::temp_buf;
99
use crate::rows::DynamicRows;
1010
use crate::Attributes;
11-
use fallible_collections::FallibleVec;
1211
use rgb::ComponentSlice;
1312
use std::collections::{HashMap, HashSet};
1413
use std::convert::TryInto;
@@ -262,7 +261,8 @@ impl Histogram {
262261
debug_assert!(gamma > 0.);
263262

264263
let mut counts = [0; LIQ_MAXCLUSTER];
265-
let mut temp: Vec<_> = FallibleVec::try_with_capacity(self.hashmap.len())?;
264+
let mut temp = Vec::new();
265+
temp.try_reserve_exact(self.hashmap.len())?;
266266
// Limit perceptual weight to 1/10th of the image surface area to prevent
267267
// a single color from dominating all others.
268268
let max_perceptual_weight = 0.1 * self.total_area as f32;
@@ -308,7 +308,8 @@ impl Histogram {
308308
next_begin += count;
309309
}
310310

311-
let mut items: Vec<_> = FallibleVec::try_with_capacity(temp.len())?;
311+
let mut items = Vec::new();
312+
items.try_reserve_exact(temp.len())?;
312313
items.resize(temp.len(), HistItem {
313314
color: if cfg!(debug_assertions) { f_pixel( ARGBF { r:f32::NAN, g:f32::NAN, b:f32::NAN, a:f32::NAN } ) } else { f_pixel::default() },
314315
adjusted_weight: if cfg!(debug_assertions) { f32::NAN } else { 0. },

src/image.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::rows::{DynamicRows, PixelsSource};
77
use crate::seacow::RowBitmap;
88
use crate::seacow::SeaCow;
99
use crate::LIQ_HIGH_MEMORY_LIMIT;
10-
use fallible_collections::FallibleVec;
1110
use rgb::ComponentMap;
1211
use std::mem::MaybeUninit;
1312

@@ -205,7 +204,8 @@ impl<'pixels> Image<'pixels> {
205204
pub fn add_fixed_color(&mut self, color: RGBA) -> Result<(), Error> {
206205
if self.fixed_colors.len() >= MAX_COLORS { return Err(Unsupported); }
207206
let lut = gamma_lut(self.px.gamma);
208-
self.fixed_colors.try_push(f_pixel::from_rgba(&lut, RGBA {r: color.r, g: color.g, b: color.b, a: color.a}))?;
207+
self.fixed_colors.try_reserve(1)?;
208+
self.fixed_colors.push(f_pixel::from_rgba(&lut, RGBA {r: color.r, g: color.g, b: color.b, a: color.a}));
209209
Ok(())
210210
}
211211

@@ -335,7 +335,8 @@ impl<'pixels> Image<'pixels> {
335335
}
336336

337337
fn try_zero_vec(len: usize) -> Result<Vec<u8>, Error> {
338-
let mut vec: Vec<_> = FallibleVec::try_with_capacity(len)?;
338+
let mut vec = Vec::new();
339+
vec.try_reserve_exact(len)?;
339340
vec.resize(len, 0);
340341
Ok(vec)
341342
}

src/kmeans.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use crate::Error;
21
use crate::hist::{HistItem, HistogramInternal};
32
use crate::nearest::Nearest;
4-
use crate::pal::{PalF, PalIndex, PalPop, f_pixel};
5-
use fallible_collections::FallibleVec;
3+
use crate::pal::{f_pixel, PalF, PalIndex, PalPop};
4+
use crate::rayoff::*;
5+
use crate::Error;
66
use rgb::alt::ARGB;
77
use rgb::ComponentMap;
88
use std::cell::RefCell;
9-
use crate::rayoff::*;
109

1110
pub(crate) struct Kmeans {
1211
averages: Vec<ColorAvg>,
@@ -23,7 +22,8 @@ struct ColorAvg {
2322
impl Kmeans {
2423
#[inline]
2524
pub fn new(pal_len: usize) -> Result<Self, Error> {
26-
let mut averages: Vec<_> = FallibleVec::try_with_capacity(pal_len)?;
25+
let mut averages = Vec::new();
26+
averages.try_reserve_exact(pal_len)?;
2727
averages.resize(pal_len, ColorAvg::default());
2828
Ok(Self {
2929
averages,

src/mediancut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::pal::{f_pixel, PalF, PalPop};
33
use crate::pal::{PalLen, ARGBF};
44
use crate::quant::quality_to_mse;
55
use crate::{OrdFloat, Error};
6-
use fallible_collections::FallibleVec;
76
use rgb::ComponentMap;
87
use rgb::ComponentSlice;
98
use std::cmp::Reverse;
@@ -232,7 +231,8 @@ impl<'hist> MedianCutter<'hist> {
232231
debug_assert!(hist.clusters.last().unwrap().end as usize == hist.items.len());
233232

234233
let mut hist_items = &mut hist.items[..];
235-
let mut boxes: Vec<_> = FallibleVec::try_with_capacity(target_colors as usize)?;
234+
let mut boxes = Vec::new();
235+
boxes.try_reserve(target_colors as usize)?;
236236

237237
let used_boxes = hist.clusters.iter().filter(|b| b.begin != b.end).count();
238238
if used_boxes <= target_colors as usize / 3 {

src/nearest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::pal::MAX_COLORS;
22
use crate::pal::PalIndex;
33
use crate::pal::{f_pixel, PalF};
44
use crate::{OrdFloat, Error};
5-
use fallible_collections::FallibleVec;
65

76
impl<'pal> Nearest<'pal> {
87
#[inline(never)]
@@ -131,7 +130,8 @@ fn vp_create_node(indexes: &mut [MapIndex], items: &PalF) -> Result<Node, Error>
131130
let radius = radius_squared.sqrt();
132131

133132
let (near, far, rest) = if num_indexes < 7 {
134-
let mut rest: Vec<_> = FallibleVec::try_with_capacity(num_indexes)?;
133+
let mut rest = Vec::new();
134+
rest.try_reserve_exact(num_indexes)?;
135135
rest.extend(near.iter().chain(far.iter()).map(|i| Leaf {
136136
idx: i.idx,
137137
color: palette[usize::from(i.idx)],

src/quant.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::remap::{mse_to_standard_mse, DitherMapMode, Remapped};
99
use crate::seacow::RowBitmapMut;
1010
use crate::OrdFloat;
1111
use arrayvec::ArrayVec;
12-
use fallible_collections::FallibleVec;
1312
use std::cmp::Reverse;
1413
use std::fmt;
1514
use std::mem::MaybeUninit;
@@ -184,10 +183,11 @@ impl QuantizationResult {
184183
let len = image.width() * image.height();
185184
// Capacity is essential here, as it creates uninitialized buffer
186185
unsafe {
187-
let mut buf: Vec<u8> = FallibleVec::try_with_capacity(len)?;
188-
let uninit_slice = std::slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<MaybeUninit<u8>>(), buf.capacity());
186+
let mut buf = Vec::new();
187+
buf.try_reserve_exact(len)?;
188+
let uninit_slice = &mut buf.spare_capacity_mut()[..len];
189189
self.remap_into(image, uninit_slice)?;
190-
buf.set_len(uninit_slice.len());
190+
buf.set_len(len);
191191
Ok((self.palette_vec(), buf))
192192
}
193193
}
@@ -215,7 +215,8 @@ impl QuantizationResult {
215215
#[must_use]
216216
pub fn palette_vec(&mut self) -> Vec<RGBA> {
217217
let pal = self.palette();
218-
let mut out: Vec<RGBA> = FallibleVec::try_with_capacity(pal.len()).unwrap();
218+
let mut out: Vec<RGBA> = Vec::new();
219+
out.try_reserve_exact(pal.len()).unwrap();
219220
out.extend_from_slice(pal);
220221
out
221222
}

src/remap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::pal::{ARGBF, LIQ_WEIGHT_MSE, MIN_OPAQUE_A, PalF, PalIndex, Palette, f
66
use crate::quant::{quality_to_mse, QuantizationResult};
77
use crate::rows::temp_buf;
88
use crate::seacow::{RowBitmap, RowBitmapMut};
9-
use fallible_collections::FallibleVec;
109
use crate::rayoff::*;
1110
use std::cell::RefCell;
1211
use std::mem::MaybeUninit;
@@ -161,7 +160,8 @@ pub(crate) fn remap_to_palette_floyd(input_image: &mut Image, mut output_pixels:
161160
let mut background = input_image.background.as_mut().map(|bg| bg.px.rows_iter(&mut temp_row)).transpose()?;
162161

163162
let errwidth = width + 2; // +2 saves from checking out of bounds access
164-
let mut thiserr_data: Vec<_> = FallibleVec::try_with_capacity(errwidth * 2)?;
163+
let mut thiserr_data = Vec::new();
164+
thiserr_data.try_reserve_exact(errwidth * 2)?;
165165
thiserr_data.resize(errwidth * 2, f_pixel::default());
166166
let (mut thiserr, mut nexterr) = thiserr_data.split_at_mut(errwidth);
167167
let n = Nearest::new(&quant.palette)?;

src/rows.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use fallible_collections::FallibleVec;
2-
31
use crate::error::*;
42
use crate::pal::{f_pixel, gamma_lut, RGBA};
53
use crate::seacow::SeaCow;
@@ -197,7 +195,8 @@ impl<'pixels,'rows> DynamicRows<'pixels,'rows> {
197195
}
198196

199197
pub(crate) fn temp_buf<T>(len: usize) -> Result<Box<[MaybeUninit<T>]>, Error> {
200-
let mut v: Vec<_> = FallibleVec::try_with_capacity(len)?;
198+
let mut v = Vec::new();
199+
v.try_reserve_exact(len)?;
201200
unsafe { v.set_len(len) };
202201
Ok(v.into_boxed_slice())
203202
}

0 commit comments

Comments
 (0)