Skip to content

Commit 1ee08f0

Browse files
committed
Deprecate as_slice
1 parent ee7c6e2 commit 1ee08f0

7 files changed

Lines changed: 62 additions & 36 deletions

File tree

examples/example.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#[cfg(feature = "as-bytes")]
21
fn main() {
3-
use rgb::ComponentSlice;
42
use rgb::prelude::*;
53
use rgb::Rgb;
64

@@ -9,20 +7,11 @@ fn main() {
97
g: 0,
108
b: 100,
119
};
10+
#[cfg(feature = "bytemuck")]
1211
assert_eq!(rgb::bytemuck::cast_slice::<_, u8>(&[px])[0], 255);
1312

14-
let bigpx = Rgb::<u16> {
15-
r: 65535_u16,
16-
g: 0,
17-
b: 0,
18-
};
19-
assert_eq!(bigpx.as_slice()[0], 65535);
20-
2113
let px = Rgb::<u8>::new(255, 0, 255);
2214
let inverted: Rgb<u8> = px.map(|ch| 255 - ch);
2315

2416
println!("{inverted}"); // rgb(0,255,0)
2517
}
26-
27-
#[cfg(not(feature = "as-bytes"))]
28-
fn main() {}

src/legacy/alt.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::legacy::internal::pixel::{ComponentMap, ColorComponentMap, ComponentSlice};
1+
use crate::legacy::internal::pixel::{ComponentMap, ColorComponentMap};
2+
#[allow(deprecated)]
3+
use crate::legacy::internal::pixel::ComponentSlice;
24
use core::slice;
35

46
pub use crate::formats::gray::Gray_v08 as Gray;
@@ -170,6 +172,7 @@ impl<T: Copy, A: Copy, B> ColorComponentMap<GrayAlpha<B, A>, T, B> for GrayAlpha
170172
}
171173
}
172174

175+
#[allow(deprecated)]
173176
impl<T> ComponentSlice<T> for GrayAlpha<T> {
174177
#[inline(always)]
175178
fn as_slice(&self) -> &[T] {
@@ -186,6 +189,7 @@ impl<T> ComponentSlice<T> for GrayAlpha<T> {
186189
}
187190
}
188191

192+
#[allow(deprecated)]
189193
impl<T> ComponentSlice<T> for [GrayAlpha<T>] {
190194
#[inline]
191195
fn as_slice(&self) -> &[T] {
@@ -202,6 +206,7 @@ impl<T> ComponentSlice<T> for [GrayAlpha<T>] {
202206
}
203207
}
204208

209+
#[allow(deprecated)]
205210
impl<T> ComponentSlice<T> for Gray<T> {
206211
#[inline(always)]
207212
#[allow(deprecated)]
@@ -216,6 +221,7 @@ impl<T> ComponentSlice<T> for Gray<T> {
216221
}
217222
}
218223

224+
#[allow(deprecated)]
219225
impl<T> ComponentSlice<T> for [Gray<T>] {
220226
#[inline]
221227
fn as_slice(&self) -> &[T] {

src/legacy/internal/convert/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::pixel::{ComponentSlice, ComponentMap};
1+
#[allow(deprecated)]
2+
use super::pixel::ComponentSlice;
3+
use super::pixel::ComponentMap;
24
use crate::alt::{BGR, BGRA, GRB, Gray, GrayAlpha, ARGB, ABGR};
35
use crate::{RGB, RGBA};
46
use core::{mem, slice};
@@ -332,7 +334,8 @@ impl<T> AsRef<T> for Gray<T> {
332334
impl<T> AsRef<[T]> for RGB<T> {
333335
#[inline(always)]
334336
fn as_ref(&self) -> &[T] {
335-
self.as_slice()
337+
#[allow(deprecated)]
338+
ComponentSlice::as_slice(self)
336339
}
337340
}
338341

@@ -347,7 +350,8 @@ impl<T> AsRef<[T; 3]> for RGB<T> {
347350
impl<T> AsRef<[T]> for RGBA<T> {
348351
#[inline(always)]
349352
fn as_ref(&self) -> &[T] {
350-
self.as_slice()
353+
#[allow(deprecated)]
354+
ComponentSlice::as_slice(self)
351355
}
352356
}
353357

@@ -405,7 +409,8 @@ impl<T> AsMut<T> for Gray<T> {
405409
impl<T> AsMut<[T]> for RGB<T> {
406410
#[inline(always)]
407411
fn as_mut(&mut self) -> &mut [T] {
408-
self.as_mut_slice()
412+
#[allow(deprecated)]
413+
ComponentSlice::as_mut_slice(self)
409414
}
410415
}
411416

@@ -420,7 +425,8 @@ impl<T> AsMut<[T; 3]> for RGB<T> {
420425
impl<T> AsMut<[T]> for RGBA<T> {
421426
#[inline(always)]
422427
fn as_mut(&mut self) -> &mut [T] {
423-
self.as_mut_slice()
428+
#[allow(deprecated)]
429+
ComponentSlice::as_mut_slice(self)
424430
}
425431
}
426432

src/legacy/internal/pixel.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/// Casting the struct to slices of its components
2+
#[deprecated(note = "use `bytemuck::cast_slice()` instead")]
23
pub trait ComponentSlice<T> {
34
/// The components interpreted as an array, e.g. one `RGB` expands to 3 elements.
45
///
56
/// It's implemented for individual pixels as well as slices of pixels.
7+
#[deprecated(note = "use `bytemuck::cast_slice()` instead; or at least call it like ComponentSlice::as_slice(px)")]
68
fn as_slice(&self) -> &[T];
79

810
/// The components interpreted as a mutable array, e.g. one `RGB` expands to 3 elements.
@@ -16,6 +18,7 @@ pub trait ComponentSlice<T> {
1618
/// ```rust,ignore
1719
/// arr[..].as_mut_slice()
1820
/// ```
21+
#[deprecated(note = "use `bytemuck::cast_slice_mut()` instead; or at least call it like ComponentSlice::as_mut_slice(px)")]
1922
fn as_mut_slice(&mut self) -> &mut [T];
2023
}
2124

@@ -35,12 +38,14 @@ pub trait ComponentSlice<T> {
3538
///
3639
/// Plain types are not allowed to contain struct padding, booleans, chars, enums, references or pointers.
3740
#[cfg(feature = "as-bytes")]
41+
#[allow(deprecated)]
3842
pub trait ComponentBytes<T: crate::Pod> where Self: ComponentSlice<T> {
3943
/// The components interpreted as raw bytes, in machine's native endian. In `RGB` bytes of the red component are first.
4044
#[inline]
4145
fn as_bytes(&self) -> &[u8] {
4246
assert_ne!(0, core::mem::size_of::<T>());
43-
let slice = self.as_slice();
47+
#[allow(deprecated)]
48+
let slice = ComponentSlice::as_slice(self);
4449
unsafe {
4550
core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
4651
}
@@ -50,7 +55,8 @@ pub trait ComponentBytes<T: crate::Pod> where Self: ComponentSlice<T> {
5055
#[inline]
5156
fn as_bytes_mut(&mut self) -> &mut [u8] {
5257
assert_ne!(0, core::mem::size_of::<T>());
53-
let slice = self.as_mut_slice();
58+
#[allow(deprecated)]
59+
let slice = ComponentSlice::as_mut_slice(self);
5460
unsafe {
5561
core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), core::mem::size_of_val(slice))
5662
}

src/legacy/internal/rgb.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use super::pixel::{ComponentSlice, ComponentMap, ColorComponentMap};
1+
#[allow(deprecated)]
2+
use super::pixel::ComponentSlice;
3+
use super::pixel::{ComponentMap, ColorComponentMap};
24
#[cfg(feature = "as-bytes")]
3-
use super::pixel::{ComponentBytes};
5+
use super::pixel::ComponentBytes;
46
use crate::alt::GRB;
57
use crate::alt::{BGR, BGRA};
68
use crate::{RGB, RGBA};
@@ -21,7 +23,8 @@ macro_rules! impl_rgb {
2123
/// Iterate over color components (R, G, and B)
2224
#[inline(always)]
2325
pub fn iter(&self) -> core::iter::Cloned<core::slice::Iter<'_, T>> {
24-
self.as_slice().iter().cloned()
26+
#[allow(deprecated)]
27+
ComponentSlice::as_slice(self).iter().cloned()
2528
}
2629
}
2730

@@ -49,6 +52,7 @@ macro_rules! impl_rgb {
4952
}
5053
}
5154

55+
#[allow(deprecated)]
5256
impl<T> ComponentSlice<T> for $RGB<T> {
5357
#[inline(always)]
5458
fn as_slice(&self) -> &[T] {
@@ -65,6 +69,7 @@ macro_rules! impl_rgb {
6569
}
6670
}
6771

72+
#[allow(deprecated)]
6873
impl<T> ComponentSlice<T> for [$RGB<T>] {
6974
#[inline]
7075
fn as_slice(&self) -> &[T] {

src/legacy/internal/rgba.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use super::pixel::{ComponentSlice, ComponentMap, ColorComponentMap};
1+
#[allow(deprecated)]
2+
use super::pixel::ComponentSlice;
3+
use super::pixel::{ComponentMap, ColorComponentMap};
24
#[cfg(feature = "as-bytes")]
3-
use super::pixel::{ComponentBytes};
5+
use super::pixel::ComponentBytes;
46
use crate::alt::{BGRA, ARGB, ABGR, BGR};
57
use crate::{RGB, RGBA};
68
use core::fmt;
@@ -83,7 +85,8 @@ macro_rules! impl_rgba {
8385
/// Iterate over all components (length=4)
8486
#[inline(always)]
8587
pub fn iter(&self) -> core::iter::Cloned<core::slice::Iter<'_, T>> {
86-
self.as_slice().iter().cloned()
88+
#[allow(deprecated)]
89+
ComponentSlice::as_slice(self).iter().cloned()
8790
}
8891
}
8992

@@ -168,6 +171,7 @@ macro_rules! impl_rgba {
168171
}
169172
}
170173

174+
#[allow(deprecated)]
171175
impl<T> ComponentSlice<T> for $RGBA<T> {
172176
#[inline(always)]
173177
fn as_slice(&self) -> &[T] {
@@ -184,6 +188,7 @@ macro_rules! impl_rgba {
184188
}
185189
}
186190

191+
#[allow(deprecated)]
187192
impl<T> ComponentSlice<T> for [$RGBA<T>] {
188193
#[inline]
189194
fn as_slice(&self) -> &[T] {

tests/v08.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ use rgb::Bgra;
44
use rgb::Bgr;
55
#[cfg(feature = "as-bytes")]
66
use rgb::ComponentBytes;
7+
#[allow(deprecated)]
8+
use rgb::ComponentSlice;
79
use rgb::alt::{ABGR, ARGB, BGR, BGRA};
8-
use rgb::{AsPixels, ComponentSlice, FromSlice, RGB, RGB16, RGB8, RGBA, RGBA16, RGBA8, Rgb, Rgba};
10+
use rgb::{AsPixels, FromSlice, RGB, RGB16, RGB8, RGBA, RGBA16, RGBA8, Rgb, Rgba};
911
use rgb::prelude::*;
1012

1113
#[test]
14+
#[allow(deprecated)]
1215
fn rgb_works() {
1316
let rgb = RGB{r:0u8,g:128,b:255}.clone();
1417
assert_eq!(rgb.b, 255);
@@ -25,7 +28,7 @@ fn rgb_works() {
2528

2629
let rgb = RGB16{r:0u16,g:0x7F7F,b:65535};
2730
assert_eq!(rgb.b, 65535);
28-
assert_eq!(rgb.as_slice()[1], 0x7F7F);
31+
assert_eq!(ComponentSlice::as_slice(&rgb)[1], 0x7F7F);
2932

3033
#[cfg(feature = "as-bytes")]
3134
{
@@ -69,6 +72,7 @@ fn rgba_works() {
6972
}
7073

7174
#[test]
75+
#[allow(deprecated)]
7276
fn bytes() {
7377
let rgb = RGB8::new(1,2,3);
7478

@@ -102,15 +106,16 @@ fn bytes() {
102106
}
103107

104108
let rgb = RGB16::new(1,2,3);
105-
let rgb_slice = rgb.as_slice();
109+
#[allow(deprecated)]
110+
let rgb_slice = ComponentSlice::as_slice(&rgb);
106111
assert_eq!(&[1,2,3], rgb_slice);
107112
assert_eq!(rgb_slice.as_rgba(), &[]);
108113
assert_eq!(&[rgb], rgb_slice.as_rgb());
109114
assert_eq!(rgb, rgb_slice.into_iter().cloned().collect());
110115
assert_eq!(rgb, rgb_slice.iter().copied().collect());
111116

112117
let rgba = RGBA16::new(1,2,3,4);
113-
let rgba_slice = rgba.as_slice();
118+
let rgba_slice = ComponentSlice::as_slice(&rgba);
114119
assert_eq!(&[1,2,3,4], rgba_slice);
115120
assert_eq!(&[1,2,3], rgba_slice.as_rgb()[0].as_slice());
116121
assert_eq!(&[rgba], rgba_slice.as_rgba());
@@ -368,14 +373,15 @@ mod rgb_test {
368373
}
369374

370375
#[test]
376+
#[allow(deprecated)]
371377
fn sanity_check() {
372378
let neg = RGB::new(1,2,3i32).map(|x| -x);
373379
assert_eq!(neg.r, -1);
374380
assert_eq!(neg.g, -2);
375381
assert_eq!(neg.b, -3);
376382

377383
let mut px = RGB::new(3,4,5);
378-
px.as_mut_slice()[1] = 111;
384+
ComponentSlice::as_mut_slice(&mut px)[1] = 111;
379385
assert_eq!(111, px.g);
380386

381387
assert_eq!(RGBA::new(250,251,252,253), RGB::new(250,251,252).with_alpha(253));
@@ -415,6 +421,7 @@ mod rgb_test {
415421
}
416422

417423
#[test]
424+
#[allow(deprecated)]
418425
fn rgba_test() {
419426
let neg = RGBA::new(1,2,3i32,1000).map(|x| -x);
420427
assert_eq!(neg.r, -1);
@@ -425,7 +432,7 @@ fn rgba_test() {
425432
assert_eq!(neg.rgb().b, -3);
426433
assert_eq!(neg.a, -1000);
427434
assert_eq!(neg.map_alpha(|x| x+1).a, -999);
428-
assert_eq!(neg, neg.as_slice().iter().copied().collect());
435+
assert_eq!(neg, ComponentSlice::as_slice(&neg).iter().copied().collect());
429436
assert!(neg < RGBA::new(0,0,0,0));
430437

431438
#[allow(deprecated)]
@@ -437,7 +444,7 @@ fn rgba_test() {
437444
assert_eq!(4u8, neg.a);
438445

439446
let mut px = RGBA{r:1,g:2,b:3,a:4};
440-
px.as_mut_slice()[3] = 100;
447+
ComponentSlice::as_mut_slice(&mut px)[3] = 100;
441448
assert_eq!(1, px.rgb_mut().r);
442449
assert_eq!(2, px.rgb_mut().g);
443450
px.rgb_mut().b = 4;
@@ -453,6 +460,7 @@ fn rgba_test() {
453460

454461
#[test]
455462
#[cfg(feature = "as-bytes")]
463+
#[allow(deprecated)]
456464
fn abgr_test() {
457465
let abgr = ABGR {r:1,g:2,b:3,a:4};
458466
assert_eq!(4, abgr.as_slice()[0]);
@@ -464,7 +472,7 @@ fn abgr_test() {
464472
#[allow(deprecated)]
465473
fn bgra_test() {
466474
let neg = BGRA::new(1, 2, 3i32, 1000).map(|x| -x);
467-
let _ = neg.as_slice();
475+
let _ = ComponentSlice::as_slice(&neg);
468476

469477
#[cfg(feature = "as-bytes")]
470478
{
@@ -477,7 +485,7 @@ fn bgra_test() {
477485
assert_eq!(neg.b, -3);
478486
assert_eq!(neg.bgr().b, -3);
479487
assert_eq!(neg.a, -1000);
480-
assert_eq!(&[-3,-2,-1,-1000], neg.as_slice());
488+
assert_eq!(&[-3,-2,-1,-1000], ComponentSlice::as_slice(&neg));
481489
assert!(neg < BGRA::new(0, 0, 0, 0));
482490

483491
let neg = BGRA::new(1u8, 2u8, 3u8, 4u8).map_rgb(|c| -i16::from(c));
@@ -489,7 +497,7 @@ fn bgra_test() {
489497
assert_eq!(4u8, neg.a);
490498

491499
let mut px = BGRA{r:1,g:2,b:3,a:-9}.alpha(4);
492-
px.as_mut_slice()[3] = 100;
500+
ComponentSlice::as_mut_slice(&mut px)[3] = 100;
493501
assert_eq!(1, px.bgr_mut().r);
494502
assert_eq!(2, px.bgr_mut().g);
495503
px.bgr_mut().b = 4;
@@ -537,6 +545,7 @@ fn argb_converts() {
537545
}
538546

539547
#[test]
548+
#[allow(deprecated)]
540549
fn converts() {
541550
assert_eq!([1,2].as_gray(), [Gray::new(1), Gray::new(2)]);
542551
assert_eq!([3].as_gray_mut(), [Gray::new(3)]);

0 commit comments

Comments
 (0)