1
1
use core:: mem:: { transmute_copy, ManuallyDrop } ;
2
2
3
+ #[ cfg( feature = "alloc" ) ]
4
+ use alloc:: { boxed:: Box , vec:: Vec } ;
5
+
3
6
pub use palette_derive:: ArrayCast ;
4
7
5
8
use crate :: ArrayExt ;
@@ -958,7 +961,7 @@ where
958
961
/// let color2 = Box::new(Srgb::new(23u8, 198, 76));
959
962
/// let array2 = <Box<[_; 3]>>::from(color2);
960
963
/// ```
961
- #[ cfg( feature = "std " ) ]
964
+ #[ cfg( feature = "alloc " ) ]
962
965
#[ inline]
963
966
pub fn into_array_box < T > ( value : Box < T > ) -> Box < T :: Array >
964
967
where
@@ -1000,7 +1003,7 @@ where
1000
1003
/// let array2 = Box::new([23, 198, 76]);
1001
1004
/// let color2 = <Box<Srgb<u8>>>::from(array2);
1002
1005
/// ```
1003
- #[ cfg( feature = "std " ) ]
1006
+ #[ cfg( feature = "alloc " ) ]
1004
1007
#[ inline]
1005
1008
pub fn from_array_box < T > ( value : Box < T :: Array > ) -> Box < T >
1006
1009
where
@@ -1030,7 +1033,7 @@ where
1030
1033
/// vec![[64, 139, 10], [93, 18, 214]].into_boxed_slice()
1031
1034
/// )
1032
1035
/// ```
1033
- #[ cfg( feature = "std " ) ]
1036
+ #[ cfg( feature = "alloc " ) ]
1034
1037
#[ inline]
1035
1038
pub fn into_array_slice_box < T > ( values : Box < [ T ] > ) -> Box < [ T :: Array ] >
1036
1039
where
@@ -1053,7 +1056,7 @@ where
1053
1056
/// vec![64, 139, 10, 93, 18, 214].into_boxed_slice()
1054
1057
/// )
1055
1058
/// ```
1056
- #[ cfg( feature = "std " ) ]
1059
+ #[ cfg( feature = "alloc " ) ]
1057
1060
#[ inline]
1058
1061
pub fn into_component_slice_box < T > ( values : Box < [ T ] > ) -> Box < [ <T :: Array as ArrayExt >:: Item ] >
1059
1062
where
@@ -1076,7 +1079,7 @@ where
1076
1079
/// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice()
1077
1080
/// )
1078
1081
/// ```
1079
- #[ cfg( feature = "std " ) ]
1082
+ #[ cfg( feature = "alloc " ) ]
1080
1083
#[ inline]
1081
1084
pub fn from_array_slice_box < T > ( values : Box < [ T :: Array ] > ) -> Box < [ T ] >
1082
1085
where
@@ -1116,7 +1119,7 @@ where
1116
1119
/// let components = vec![64, 139, 10, 93, 18, 214, 0, 123].into_boxed_slice();
1117
1120
/// cast::from_component_slice_box::<Srgb<u8>>(components);
1118
1121
/// ```
1119
- #[ cfg( feature = "std " ) ]
1122
+ #[ cfg( feature = "alloc " ) ]
1120
1123
#[ inline]
1121
1124
pub fn from_component_slice_box < T > ( values : Box < [ <T :: Array as ArrayExt >:: Item ] > ) -> Box < [ T ] >
1122
1125
where
@@ -1162,7 +1165,7 @@ where
1162
1165
/// unreachable!();
1163
1166
/// }
1164
1167
/// ```
1165
- #[ cfg( feature = "std " ) ]
1168
+ #[ cfg( feature = "alloc " ) ]
1166
1169
#[ inline]
1167
1170
pub fn try_from_component_slice_box < T > (
1168
1171
values : Box < [ <T :: Array as ArrayExt >:: Item ] > ,
@@ -1191,7 +1194,7 @@ where
1191
1194
/// vec![[64, 139, 10], [93, 18, 214]]
1192
1195
/// )
1193
1196
/// ```
1194
- #[ cfg( feature = "std " ) ]
1197
+ #[ cfg( feature = "alloc " ) ]
1195
1198
#[ inline]
1196
1199
pub fn into_array_vec < T > ( values : Vec < T > ) -> Vec < T :: Array >
1197
1200
where
@@ -1223,7 +1226,7 @@ where
1223
1226
/// vec![64, 139, 10, 93, 18, 214]
1224
1227
/// )
1225
1228
/// ```
1226
- #[ cfg( feature = "std " ) ]
1229
+ #[ cfg( feature = "alloc " ) ]
1227
1230
#[ inline]
1228
1231
pub fn into_component_vec < T > ( values : Vec < T > ) -> Vec < <T :: Array as ArrayExt >:: Item >
1229
1232
where
@@ -1257,7 +1260,7 @@ where
1257
1260
/// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
1258
1261
/// )
1259
1262
/// ```
1260
- #[ cfg( feature = "std " ) ]
1263
+ #[ cfg( feature = "alloc " ) ]
1261
1264
#[ inline]
1262
1265
pub fn from_array_vec < T > ( values : Vec < T :: Array > ) -> Vec < T >
1263
1266
where
@@ -1316,7 +1319,7 @@ where
1316
1319
/// components.reserve_exact(2); // Not a multiple of 3
1317
1320
/// cast::from_component_vec::<Srgb<u8>>(components);
1318
1321
/// ```
1319
- #[ cfg( feature = "std " ) ]
1322
+ #[ cfg( feature = "alloc " ) ]
1320
1323
#[ inline]
1321
1324
pub fn from_component_vec < T > ( values : Vec < <T :: Array as ArrayExt >:: Item > ) -> Vec < T >
1322
1325
where
@@ -1376,7 +1379,7 @@ where
1376
1379
/// unreachable!();
1377
1380
/// }
1378
1381
/// ```
1379
- #[ cfg( feature = "std " ) ]
1382
+ #[ cfg( feature = "alloc " ) ]
1380
1383
#[ inline]
1381
1384
pub fn try_from_component_vec < T > (
1382
1385
values : Vec < <T :: Array as ArrayExt >:: Item > ,
@@ -1419,7 +1422,7 @@ where
1419
1422
/// Map values of color A to values of color B without creating a new `Vec`.
1420
1423
///
1421
1424
/// This uses the guarantees of [`ArrayCast`] to reuse the allocation.
1422
- #[ cfg( feature = "std " ) ]
1425
+ #[ cfg( feature = "alloc " ) ]
1423
1426
#[ inline]
1424
1427
pub fn map_vec_in_place < A , B , F > ( values : Vec < A > , mut map : F ) -> Vec < B >
1425
1428
where
@@ -1453,7 +1456,7 @@ where
1453
1456
/// Map values of color A to values of color B without creating a new `Box<[B]>`.
1454
1457
///
1455
1458
/// This uses the guarantees of [`ArrayCast`] to reuse the allocation.
1456
- #[ cfg( feature = "std " ) ]
1459
+ #[ cfg( feature = "alloc " ) ]
1457
1460
#[ inline]
1458
1461
pub fn map_slice_box_in_place < A , B , F > ( values : Box < [ A ] > , mut map : F ) -> Box < [ B ] >
1459
1462
where
@@ -1499,20 +1502,20 @@ impl std::error::Error for SliceCastError {}
1499
1502
1500
1503
/// The error type returned when casting a boxed slice of components fails.
1501
1504
#[ derive( Clone , PartialEq , Eq ) ]
1502
- #[ cfg( feature = "std " ) ]
1505
+ #[ cfg( feature = "alloc " ) ]
1503
1506
pub struct BoxedSliceCastError < T > {
1504
1507
/// The original values.
1505
1508
pub values : Box < [ T ] > ,
1506
1509
}
1507
1510
1508
- #[ cfg( feature = "std " ) ]
1511
+ #[ cfg( feature = "alloc " ) ]
1509
1512
impl < T > core:: fmt:: Debug for BoxedSliceCastError < T > {
1510
1513
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1511
1514
f. debug_struct ( "BoxedSliceCastError" ) . finish ( )
1512
1515
}
1513
1516
}
1514
1517
1515
- #[ cfg( feature = "std " ) ]
1518
+ #[ cfg( feature = "alloc " ) ]
1516
1519
impl < T > core:: fmt:: Display for BoxedSliceCastError < T > {
1517
1520
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1518
1521
f. write_str ( "could not convert boxed component slice to colors" )
@@ -1524,7 +1527,7 @@ impl<T> std::error::Error for BoxedSliceCastError<T> {}
1524
1527
1525
1528
/// The error type returned when casting a `Vec` of components fails.
1526
1529
#[ derive( Clone , PartialEq , Eq ) ]
1527
- #[ cfg( feature = "std " ) ]
1530
+ #[ cfg( feature = "alloc " ) ]
1528
1531
pub struct VecCastError < T > {
1529
1532
/// The type of error that occurred.
1530
1533
pub kind : VecCastErrorKind ,
@@ -1533,7 +1536,7 @@ pub struct VecCastError<T> {
1533
1536
pub values : Vec < T > ,
1534
1537
}
1535
1538
1536
- #[ cfg( feature = "std " ) ]
1539
+ #[ cfg( feature = "alloc " ) ]
1537
1540
impl < T > core:: fmt:: Debug for VecCastError < T > {
1538
1541
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1539
1542
f. debug_struct ( "VecCastError" )
@@ -1542,7 +1545,7 @@ impl<T> core::fmt::Debug for VecCastError<T> {
1542
1545
}
1543
1546
}
1544
1547
1545
- #[ cfg( feature = "std " ) ]
1548
+ #[ cfg( feature = "alloc " ) ]
1546
1549
impl < T > core:: fmt:: Display for VecCastError < T > {
1547
1550
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1548
1551
f. write_str ( "could not convert component vector to colors" )
@@ -1554,7 +1557,7 @@ impl<T> std::error::Error for VecCastError<T> {}
1554
1557
1555
1558
/// The type of error that is returned when casting a `Vec` of components.
1556
1559
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
1557
- #[ cfg( feature = "std " ) ]
1560
+ #[ cfg( feature = "alloc " ) ]
1558
1561
pub enum VecCastErrorKind {
1559
1562
/// The type of error returned when the length of a `Vec` didn't match the
1560
1563
/// requirements.
@@ -1567,9 +1570,10 @@ pub enum VecCastErrorKind {
1567
1570
1568
1571
#[ cfg( test) ]
1569
1572
mod test {
1573
+ #[ cfg( feature = "alloc" ) ]
1570
1574
use crate :: { LinSrgb , Srgb } ;
1571
1575
1572
- #[ cfg( feature = "std " ) ]
1576
+ #[ cfg( feature = "alloc " ) ]
1573
1577
#[ test]
1574
1578
fn array_vec_len_cap ( ) {
1575
1579
let mut original = vec ! [
@@ -1596,11 +1600,12 @@ mod test {
1596
1600
assert_eq ! ( colors. capacity( ) , 8 ) ;
1597
1601
}
1598
1602
1603
+ #[ cfg( feature = "alloc" ) ]
1599
1604
#[ test]
1600
1605
fn map_vec_in_place ( ) {
1601
1606
fn do_things ( rgb : Srgb ) -> LinSrgb {
1602
1607
let mut linear = rgb. into_linear ( ) ;
1603
- std :: mem:: swap ( & mut linear. red , & mut linear. blue ) ;
1608
+ core :: mem:: swap ( & mut linear. red , & mut linear. blue ) ;
1604
1609
linear
1605
1610
}
1606
1611
@@ -1615,11 +1620,12 @@ mod test {
1615
1620
)
1616
1621
}
1617
1622
1623
+ #[ cfg( feature = "alloc" ) ]
1618
1624
#[ test]
1619
1625
fn map_slice_box_in_place ( ) {
1620
1626
fn do_things ( rgb : Srgb ) -> LinSrgb {
1621
1627
let mut linear = rgb. into_linear ( ) ;
1622
- std :: mem:: swap ( & mut linear. red , & mut linear. blue ) ;
1628
+ core :: mem:: swap ( & mut linear. red , & mut linear. blue ) ;
1623
1629
linear
1624
1630
}
1625
1631
0 commit comments