Skip to content

Commit

Permalink
remove Copy from Color, add must_use annotations throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
apparebit committed Jun 8, 2024
1 parent 77a437f commit 5e27ee0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub use super::util::Coordinate;
///
/// While rounding isn't strictly necessary for correctness, it makes for a more
/// robust comparison without meaningfully reducing precision.
#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug)]
pub struct Color {
space: ColorSpace,
coordinates: [f64; 3],
Expand Down Expand Up @@ -132,6 +132,8 @@ impl Color {
}
}

// ----------------------------------------------------------------------------------------------------------------

/// Access the color space.
///
/// ```
Expand All @@ -154,6 +156,8 @@ impl Color {
&self.coordinates
}

// ----------------------------------------------------------------------------------------------------------------

/// Convert this color to the target color space.
///
///
Expand Down Expand Up @@ -236,6 +240,7 @@ impl Color {
/// two three-hop conversion functions). In short, by performing *limited*
/// dynamic look-ups, we can get most of the benefits of a fully specialized
/// implementation.
#[must_use = "method returns a new color and does not mutate original value"]
pub fn to(&self, target: ColorSpace) -> Self {
Self {
space: target,
Expand Down Expand Up @@ -274,6 +279,7 @@ impl Color {
/// let green = too_green.clip();
/// assert!(green.in_gamut());
/// ```
#[must_use = "method returns a new color and does not mutate original value"]
pub fn clip(&self) -> Self {
Self {
space: self.space,
Expand Down Expand Up @@ -319,6 +325,7 @@ impl Color {
/// let yellow = too_yellow.map_to_gamut();
/// assert!(yellow.in_gamut());
/// ```
#[must_use = "method returns a new color and does not mutate original value"]
pub fn map_to_gamut(&self) -> Self {
Self {
space: self.space,
Expand All @@ -340,7 +347,8 @@ impl Color {
// --------------------------------------------------------------------------------------------------------------------

impl Default for Color {
/// Create an instance of the default color, which is black in XYZ.
/// Create an instance of the default color. The chosen default for this
/// crate is pitch black, i.e., the origin in XYZ.
fn default() -> Self {
Color {
space: ColorSpace::Xyz,
Expand Down
1 change: 1 addition & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl EmbeddedRgb {
/// Update the named coordinate to the given value. This struct implements
/// this method in lieu of `index_mut()`, which cannot enforce the invariant
/// that coordinates must be between 0 and 5, inclusive.
#[must_use = "method fails to update coordinate if value is out of bounds"]
pub fn update(&mut self, index: Coordinate, value: u8) -> Result<(), OutOfBoundsError> {
if value <= 5 {
self.0[index as usize] = value;
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub use format::TrueColor;
/// ANSI colors do not have intrinsic color values, so we provide them through
/// the [`current_theme`]. In addition to the 16 extended ANSI colors, a theme
/// includes two more colors for the foreground and background defaults.
#[derive(Clone,Debug)]
pub struct Theme {
#[allow(dead_code)]
foreground: Color,
Expand Down Expand Up @@ -161,7 +162,9 @@ impl From<AnsiColor> for Color {
/// color theme.
fn from(value: AnsiColor) -> Color {
let theme = current_theme();
*theme.ansi(value)
// From<EmbeddedRgb> and From<GrayGradient> create a new color objects.
// We do the same here, just with an explicit clone().
theme.ansi(value).clone()
}
}

Expand Down

0 comments on commit 5e27ee0

Please sign in to comment.