Skip to content

Commit 1b143b5

Browse files
Replace trait Arraylike with AsRef<[T]>
1 parent a7cdf75 commit 1b143b5

File tree

2 files changed

+25
-63
lines changed

2 files changed

+25
-63
lines changed

palette/build/named.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn build_gradients(writer: &mut File) {
9595
.unwrap_or_else(|| panic!("couldn't get the {}th blue-value for {}", i, name));
9696
write!(writer, "({:.10},{}{{red: {}, green: {}, blue: {}, standard: ::core::marker::PhantomData}}),", (i as f32/number_of_colors as f32), color_type, red, green, blue).unwrap();
9797
}
98-
write!(writer, "]);\n").unwrap();
98+
write!(writer, "], ::core::marker::PhantomData);\n").unwrap();
9999
}
100100
}
101101

palette/src/gradient/mod.rs

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! default).
55
66
use std::cmp::max;
7-
use std::ops::Index;
7+
use core::marker::PhantomData;
88

99
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
1010
use num_traits::{One, Zero};
@@ -16,55 +16,13 @@ use crate::{from_f64, FromF64};
1616
#[cfg(feature = "named_gradients")]
1717
pub mod named;
1818

19-
/// Types which represent an ordered collection of known size
20-
pub trait ArrayLike : Index<usize>{
21-
/// Returns the number of elements in the collection, also referred to as its 'length'.
22-
fn len(&self) -> usize;
23-
/// Returns a reference to the element at the position i or None if out of bounds.
24-
fn get(&self, i: usize) -> Option<&Self::Output>;
25-
/// Returns a pointer to the first element of the collection, or None if it is empty.
26-
fn first(&self) -> Option<&Self::Output> {
27-
self.get(0)
28-
}
29-
/// Returns a pointer to the last element of the collection, or None if it is empty.
30-
fn last(&self) -> Option<&Self::Output> {
31-
if !self.is_empty() {
32-
self.get(self.len() - 1)
33-
} else {
34-
None
35-
}
36-
}
37-
/// Returns true if the collection contains no elements.
38-
fn is_empty(&self) -> bool {
39-
self.len() == 0
40-
}
41-
}
42-
43-
impl<T> ArrayLike for Vec<T> {
44-
fn len(&self) -> usize {
45-
self.len()
46-
}
47-
fn get(&self, i: usize) -> Option<&T> {
48-
self.as_slice().get(i)
49-
}
50-
}
51-
52-
impl<T, const N: usize> ArrayLike for [T;N] {
53-
fn len(&self) -> usize {
54-
<[T]>::len(self)
55-
}
56-
fn get(&self, i: usize) -> Option<&T> {
57-
<[T]>::get(self,i)
58-
}
59-
}
60-
6119
impl<C,T> From<T> for Gradient<C,T>
6220
where
6321
C: Mix + Clone,
64-
T: ArrayLike<Output = (C::Scalar, C)>
22+
T: AsRef<[(C::Scalar, C)]>
6523
{
6624
fn from(col: T) -> Self {
67-
Gradient(col)
25+
Gradient(col, PhantomData)
6826
}
6927
}
7028

@@ -77,21 +35,22 @@ where
7735
/// the domain of the gradient will have the same color as the closest control
7836
/// point.
7937
#[derive(Clone, Debug)]
80-
pub struct Gradient<C, T = Vec<(<C as Mix>::Scalar, C)>>(T)
38+
pub struct Gradient<C, T = Vec<(<C as Mix>::Scalar, C)>>(T, PhantomData<C>)
8139
where
8240
C: Mix + Clone,
83-
T: ArrayLike<Output = (C::Scalar, C)>;
41+
T: AsRef<[(C::Scalar, C)]>;
8442

8543
impl<C,T> Gradient<C,T>
8644
where
8745
C: Mix + Clone,
88-
T: ArrayLike<Output = (C::Scalar, C)>
46+
T: AsRef<[(C::Scalar, C)]>
8947
{
9048
/// Get a color from the gradient. The color of the closest control point
9149
/// will be returned if `i` is outside the domain.
9250
pub fn get(&self, i: C::Scalar) -> C {
9351
let &(mut min, ref min_color) = self
9452
.0
53+
.as_ref()
9554
.get(0)
9655
.expect("a Gradient must contain at least one color");
9756
let mut min_color = min_color;
@@ -103,10 +62,11 @@ where
10362

10463
let &(mut max, ref max_color) = self
10564
.0
65+
.as_ref()
10666
.last()
10767
.expect("a Gradient must contain at least one color");
10868
let mut max_color = max_color;
109-
let mut max_index = self.0.len() - 1;
69+
let mut max_index = self.0.as_ref().len() - 1;
11070

11171
if i >= max {
11272
return max_color.clone();
@@ -115,7 +75,7 @@ where
11575
while min_index < max_index - 1 {
11676
let index = min_index + (max_index - min_index) / 2;
11777

118-
let (p, ref color) = self.0[index];
78+
let (p, ref color) = self.0.as_ref()[index];
11979

12080
if i <= p {
12181
max = p;
@@ -137,10 +97,10 @@ where
13797
/// be at least one color and they are expected to be ordered by their
13898
/// position value.
13999
pub fn with_domain(colors: T) -> Gradient<C, T> {
140-
assert!(!colors.is_empty());
100+
assert!(!colors.as_ref().is_empty());
141101

142102
//Maybe sort the colors?
143-
Gradient(colors)
103+
Gradient(colors, PhantomData)
144104
}
145105

146106
/// Take `n` evenly spaced colors from the gradient, as an iterator. The
@@ -196,10 +156,12 @@ where
196156
pub fn domain(&self) -> (C::Scalar, C::Scalar) {
197157
let &(min, _) = self
198158
.0
159+
.as_ref()
199160
.get(0)
200161
.expect("a Gradient must contain at least one color");
201162
let &(max, _) = self
202163
.0
164+
.as_ref()
203165
.last()
204166
.expect("a Gradient must contain at least one color");
205167
(min, max)
@@ -221,7 +183,7 @@ impl<C: Mix + Clone> Gradient<C> {
221183
*p = from_f64::<C::Scalar>(i as f64) * step_size;
222184
}
223185

224-
Gradient(points)
186+
Gradient(points, PhantomData)
225187
}
226188
}
227189

@@ -230,7 +192,7 @@ impl<C: Mix + Clone> Gradient<C> {
230192
pub struct Take<'a, C, T = Vec<(<C as Mix>::Scalar, C)>>
231193
where
232194
C: Mix + Clone + 'a,
233-
T: ArrayLike<Output = (C::Scalar, C)>
195+
T: AsRef<[(C::Scalar, C)]>
234196
{
235197
gradient: MaybeSlice<'a, C, T>,
236198
from: C::Scalar,
@@ -244,7 +206,7 @@ impl<'a, C, T> Iterator for Take<'a, C, T>
244206
where
245207
C::Scalar: FromF64,
246208
C: Mix + Clone,
247-
T: ArrayLike<Output = (C::Scalar, C)>
209+
T: AsRef<[(C::Scalar, C)]>
248210
{
249211
type Item = C;
250212

@@ -277,14 +239,14 @@ impl<'a, C, T> ExactSizeIterator for Take<'a, C, T>
277239
where
278240
C::Scalar: FromF64,
279241
C: Mix + Clone,
280-
T: ArrayLike<Output = (C::Scalar, C)>
242+
T: AsRef<[(C::Scalar, C)]>
281243
{}
282244

283245
impl<'a, C, T> DoubleEndedIterator for Take<'a, C, T>
284246
where
285247
C::Scalar: FromF64,
286248
C: Mix + Clone,
287-
T: ArrayLike<Output = (C::Scalar, C)>
249+
T: AsRef<[(C::Scalar, C)]>
288250
{
289251
fn next_back(&mut self) -> Option<Self::Item> {
290252
if self.from_head + self.from_end < self.len {
@@ -309,7 +271,7 @@ where
309271
pub struct Slice<'a, C,T = Vec<(<C as Mix>::Scalar, C)>>
310272
where
311273
C: Mix + Clone + 'a,
312-
T: ArrayLike<Output = (C::Scalar, C)>
274+
T: AsRef<[(C::Scalar, C)]>
313275
{
314276
gradient: &'a Gradient<C,T>,
315277
range: Range<C::Scalar>,
@@ -318,7 +280,7 @@ where
318280
impl<'a, C, T> Slice<'a, C, T>
319281
where
320282
C: Mix + Clone + 'a,
321-
T: ArrayLike<Output = (C::Scalar, C)>
283+
T: AsRef<[(C::Scalar, C)]>
322284
{
323285
/// Get a color from the gradient slice. The color of the closest domain
324286
/// limit will be returned if `i` is outside the domain.
@@ -353,7 +315,7 @@ where
353315
impl<'a, C, T> Slice<'a, C, T>
354316
where
355317
C: Mix + Clone + 'a,
356-
T: ArrayLike<Output = (C::Scalar, C)> + Clone
318+
T: AsRef<[(C::Scalar, C)]> + Clone
357319
{
358320
/// Take `n` evenly spaced colors from the gradient slice, as an iterator.
359321
pub fn take(&self, n: usize) -> Take<C, T> {
@@ -544,7 +506,7 @@ where
544506
enum MaybeSlice<'a, C, T = Vec<(<C as Mix>::Scalar, C)>>
545507
where
546508
C: Mix + Clone + 'a,
547-
T: ArrayLike<Output = (C::Scalar, C)>
509+
T: AsRef<[(C::Scalar, C)]>
548510
{
549511
NotSlice(&'a Gradient<C, T>),
550512
Slice(Slice<'a, C, T>),
@@ -553,7 +515,7 @@ where
553515
impl<'a, C, T> MaybeSlice<'a, C, T>
554516
where
555517
C: Mix + Clone + 'a,
556-
T: ArrayLike<Output = (C::Scalar, C)>
518+
T: AsRef<[(C::Scalar, C)]>
557519
{
558520
fn get(&self, i: C::Scalar) -> C {
559521
match *self {

0 commit comments

Comments
 (0)