diff options
author | Sebastian Dröge <sebastian@centricular.com> | 2019-01-25 17:42:27 +0300 |
---|---|---|
committer | Sebastian Dröge <sebastian@centricular.com> | 2019-01-25 17:42:27 +0300 |
commit | 4c618394b70f56509c7b63cc26aa3737f1b4cc3b (patch) | |
tree | ba95e45c6a6552ed342ea94e3e9c552d19386870 | |
parent | b8d19920a8dc42dd611238c4a19f3f9dec073372 (diff) |
tutorial: Use chunks_exact/chunks_exact_mut instead of the normal chunks iterator
We always want as many items as given, no less. And this also enables
the compiler to optimize the code further.
-rw-r--r-- | gst-plugin-tutorial/src/rgb2gray.rs | 22 | ||||
-rw-r--r-- | gst-plugin-tutorial/src/sinesrc.rs | 2 |
2 files changed, 12 insertions, 12 deletions
diff --git a/gst-plugin-tutorial/src/rgb2gray.rs b/gst-plugin-tutorial/src/rgb2gray.rs index 7f1029115..dd083d86a 100644 --- a/gst-plugin-tutorial/src/rgb2gray.rs +++ b/gst-plugin-tutorial/src/rgb2gray.rs @@ -489,22 +489,22 @@ impl BaseTransformImpl for Rgb2Gray { // Iterate over each line of the input and output frame, mutable for the output frame. // Each input line has in_stride bytes, each output line out_stride. We use the - // chunks/chunks_mut iterators here for getting a chunks of that many bytes per + // chunks_exact/chunks_exact_mut iterators here for getting a chunks of that many bytes per // iteration and zip them together to have access to both at the same time. for (in_line, out_line) in in_data - .chunks(in_stride) - .zip(out_data.chunks_mut(out_stride)) + .chunks_exact(in_stride) + .zip(out_data.chunks_exact_mut(out_stride)) { // Next iterate the same way over each actual pixel in each line. Every pixel is 4 - // bytes in the input and output, so we again use the chunks/chunks_mut iterators + // bytes in the input and output, so we again use the chunks_exact/chunks_exact_mut iterators // to give us each pixel individually and zip them together. // // Note that we take a sub-slice of the whole lines: each line can contain an // arbitrary amount of padding at the end (e.g. for alignment purposes) and we // don't want to process that padding. for (in_p, out_p) in in_line[..in_line_bytes] - .chunks(4) - .zip(out_line[..out_line_bytes].chunks_mut(4)) + .chunks_exact(4) + .zip(out_line[..out_line_bytes].chunks_exact_mut(4)) { assert_eq!(out_p.len(), 4); @@ -529,22 +529,22 @@ impl BaseTransformImpl for Rgb2Gray { // Iterate over each line of the input and output frame, mutable for the output frame. // Each input line has in_stride bytes, each output line out_stride. We use the - // chunks/chunks_mut iterators here for getting a chunks of that many bytes per + // chunks_exact/chunks_exact_mut iterators here for getting a chunks of that many bytes per // iteration and zip them together to have access to both at the same time. for (in_line, out_line) in in_data - .chunks(in_stride) - .zip(out_data.chunks_mut(out_stride)) + .chunks_exact(in_stride) + .zip(out_data.chunks_exact_mut(out_stride)) { // Next iterate the same way over each actual pixel in each line. Every pixel is 4 // bytes in the input and 1 byte in the output, so we again use the - // chunks/chunks_mut iterators to give us each pixel individually and zip them + // chunks_exact/chunks_exact_mut iterators to give us each pixel individually and zip them // together. // // Note that we take a sub-slice of the whole lines: each line can contain an // arbitrary amount of padding at the end (e.g. for alignment purposes) and we // don't want to process that padding. for (in_p, out_p) in in_line[..in_line_bytes] - .chunks(4) + .chunks_exact(4) .zip(out_line[..out_line_bytes].iter_mut()) { // Use our above-defined function to convert a BGRx pixel with the settings to diff --git a/gst-plugin-tutorial/src/sinesrc.rs b/gst-plugin-tutorial/src/sinesrc.rs index a819c5bb7..ffa30ffc0 100644 --- a/gst-plugin-tutorial/src/sinesrc.rs +++ b/gst-plugin-tutorial/src/sinesrc.rs @@ -172,7 +172,7 @@ impl SineSrc { let mut accumulator = *accumulator_ref; let step = two_pi * freq / rate; - for chunk in data.chunks_mut(channels as usize) { + for chunk in data.chunks_exact_mut(channels as usize) { let value = vol * F::sin(NumCast::from(accumulator).unwrap()); for sample in chunk { *sample = value; |