Skip to content

Commit

Permalink
Use #[must_use] more.
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardmonkeys committed Sep 11, 2023
1 parent 22631b7 commit 554920d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct BlendMode {
impl BlendMode {
/// Creates a new blend mode from color mixing and layer composition
/// functions.
#[must_use]
pub fn new(mix: Mix, compose: Compose) -> Self {
Self { mix, compose }
}
Expand Down
8 changes: 8 additions & 0 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,37 @@ impl<T> Blob<T> {
}

/// Consumes self and returns the inner components of the blob.
#[must_use]
pub fn into_raw_parts(self) -> (Arc<dyn AsRef<[T]> + Send + Sync>, u64) {
(self.data, self.id)
}

/// Returns the length of the data.
#[must_use]
pub fn len(&self) -> usize {
self.data().len()
}

/// Returns true if the blob is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns a reference to the underlying data.
#[must_use]
pub fn data(&self) -> &[T] {
self.data.as_ref().as_ref()
}

/// Returns the unique identifier associated with the data.
#[must_use]
pub fn id(&self) -> u64 {
self.id
}

/// Downgrades the shared blob to a weak reference.
#[must_use]
pub fn downgrade(&self) -> WeakBlob<T> {
WeakBlob {
data: Arc::downgrade(&self.data),
Expand All @@ -123,12 +129,14 @@ impl<T> Clone for WeakBlob<T> {

impl<T> WeakBlob<T> {
/// Returns the unique identifier associated with the data.
#[must_use]
pub fn id(&self) -> u64 {
self.id
}

/// Upgrades the weak reference. Returns `None` if the inner value has been
/// dropped.
#[must_use]
pub fn upgrade(&self) -> Option<Blob<T>> {
Some(Blob {
data: self.data.upgrade()?,
Expand Down
1 change: 1 addition & 0 deletions src/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub enum BrushRef<'a> {

impl<'a> BrushRef<'a> {
/// Converts the reference to an owned brush.
#[must_use]
pub fn to_owned(&self) -> Brush {
match self {
Self::Solid(color) => Brush::Solid(*color),
Expand Down
9 changes: 9 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ pub struct Color {

impl Color {
/// Creates a new RGB color with 255 alpha.
#[must_use]
pub const fn rgb8(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}

/// Creates a new RGBA color.
#[must_use]
pub const fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
Expand All @@ -32,6 +34,7 @@ impl Color {
///
/// The interpretation is the same as rgb8, and no greater precision is
/// (currently) assumed.
#[must_use]
pub fn rgb(r: f64, g: f64, b: f64) -> Self {
Self::rgba(r, g, b, 1.0)
}
Expand All @@ -40,6 +43,7 @@ impl Color {
///
/// The interpretation is the same as rgba32, and no greater precision is
/// (currently) assumed.
#[must_use]
pub fn rgba(r: f64, g: f64, b: f64, a: f64) -> Self {
let r = (r.clamp(0.0, 1.0) * 255.0).round() as u8;
let g = (g.clamp(0.0, 1.0) * 255.0).round() as u8;
Expand All @@ -65,6 +69,7 @@ impl Color {
/// Currently out-of-gamut values are clipped to the nearest sRGB color,
/// which is perhaps not ideal (the clipping might change the hue). See
/// <https://github.com/d3/d3-color/issues/33> for discussion.
#[must_use]
pub fn hlc(h: f64, l: f64, c: f64) -> Self {
Self::hlca(h, l, c, 1.0)
}
Expand All @@ -75,6 +80,7 @@ impl Color {
#[allow(non_snake_case)]
#[allow(clippy::many_single_char_names)]
#[allow(clippy::unreadable_literal)]
#[must_use]
pub fn hlca(h: f64, l: f64, c: f64, a: f64) -> Color {
let alpha = a;
// The reverse transformation from Lab to XYZ, see
Expand Down Expand Up @@ -125,19 +131,22 @@ impl Color {
///
/// Currently accepts CSS style hexadecimal colors of the forms #RGB, #RGBA,
/// #RRGGBB, #RRGGBBAA or the name of an SVG color such as "aliceblue".
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
parse_color(s)
}

/// Returns the color with the alpha component multiplied by the specified
/// factor.
#[must_use]
pub fn with_alpha_factor(self, alpha: f32) -> Self {
let mut result = self;
result.a = ((result.a as f32) * alpha).round() as u8;
result
}

/// Returns the color as a packed premultiplied value.
#[must_use]
pub fn to_premul_u32(self) -> u32 {
let a = self.a as f64 * (1.0 / 255.0);
let r = (self.r as f64 * a).round() as u32;
Expand Down
1 change: 1 addition & 0 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Font {

impl Font {
/// Creates a new font with the given data and collection index.
#[must_use]
pub fn new(data: Blob<u8>, index: u32) -> Self {
Self { data, index }
}
Expand Down
3 changes: 3 additions & 0 deletions src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl cmp::Eq for ColorStop {}
impl ColorStop {
/// Returns the color stop with the alpha component multiplied by the specified
/// factor.
#[must_use]
pub fn with_alpha_factor(self, alpha: f32) -> Self {
Self {
offset: self.offset,
Expand Down Expand Up @@ -178,12 +179,14 @@ impl Gradient {
}

/// Builder method for setting the gradient extend mode.
#[must_use]
pub fn with_extend(mut self, mode: Extend) -> Self {
self.extend = mode;
self
}

/// Builder method for setting the color stop collection.
#[must_use]
pub fn with_stops(mut self, stops: impl ColorStopsSource) -> Self {
self.stops.clear();
stops.collect_stops(&mut self.stops);
Expand Down
3 changes: 3 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl Format {
/// of the given dimensions.
///
/// A result of `None` indicates an overflow in the size calculation.
#[must_use]
pub fn size_in_bytes(self, width: u32, height: u32) -> Option<usize> {
match self {
Self::Rgba8 => 4usize
Expand All @@ -41,6 +42,7 @@ pub struct Image {

impl Image {
/// Creates a new image with the given data, [format](Format) and dimensions.
#[must_use]
pub fn new(data: Blob<u8>, format: Format, width: u32, height: u32) -> Self {
Self {
data,
Expand All @@ -52,6 +54,7 @@ impl Image {
}

/// Builder method for setting the image [extend mode](Extend).
#[must_use]
pub fn with_extend(mut self, mode: Extend) -> Self {
self.extend = mode;
self
Expand Down
9 changes: 9 additions & 0 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Default for Stroke {

impl Stroke {
/// Creates a new stroke with the specified width.
#[must_use]
pub fn new(width: f32) -> Self {
Self {
width,
Expand All @@ -82,37 +83,43 @@ impl Stroke {
}

/// Builder method for setting the join style.
#[must_use]
pub fn with_join(mut self, join: Join) -> Self {
self.join = join;
self
}

/// Builder method for setting the limit for miter joins.
#[must_use]
pub fn with_miter_limit(mut self, limit: f32) -> Self {
self.miter_limit = limit;
self
}

/// Builder method for setting the cap style for the start of the stroke.
#[must_use]
pub fn with_start_cap(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self
}

/// Builder method for setting the cap style for the end of the stroke.
#[must_use]
pub fn with_end_cap(mut self, cap: Cap) -> Self {
self.end_cap = cap;
self
}

/// Builder method for setting the cap style.
#[must_use]
pub fn with_caps(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self.end_cap = cap;
self
}

/// Builder method for setting the dashing parameters.
#[must_use]
pub fn with_dashes<P>(mut self, offset: f32, pattern: P) -> Self
where
P: IntoIterator,
Expand All @@ -127,6 +134,7 @@ impl Stroke {

/// Builder method for setting whether or not the stroke should be affected
/// by the scale of any applied transform.
#[must_use]
pub fn with_scale(mut self, yes: bool) -> Self {
self.scale = yes;
self
Expand Down Expand Up @@ -173,6 +181,7 @@ pub enum StyleRef<'a> {

impl<'a> StyleRef<'a> {
/// Converts the reference to an owned draw.
#[must_use]
pub fn to_owned(&self) -> Style {
match self {
Self::Fill(fill) => Style::Fill(*fill),
Expand Down

0 comments on commit 554920d

Please sign in to comment.