Skip to content

Commit

Permalink
Apply most clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronErhardt committed Sep 8, 2024
1 parent 99474cd commit 6290f4c
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 93 deletions.
2 changes: 1 addition & 1 deletion plotters-backend/src/rasterizer/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Edge {

impl PartialOrd for Edge {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.get_slave_pos().partial_cmp(&other.get_slave_pos())
Some(self.cmp(other))
}
}

Expand Down
2 changes: 1 addition & 1 deletion plotters-bitmap/src/bitmap_pixel/bgrx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl PixelFormat for BGRXPixel {
a: f64,
) {
let (w, h) = target.get_size();
let a = a.min(1.0).max(0.0);
let a = a.clamp(0.0, 1.0);
if a == 0.0 {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion plotters-bitmap/src/bitmap_pixel/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl PixelFormat for RGBPixel {
a: f64,
) {
let (w, h) = target.get_size();
let a = a.min(1.0).max(0.0);
let a = a.clamp(0.0, 1.0);
if a == 0.0 {
return;
}
Expand Down
38 changes: 3 additions & 35 deletions plotters-svg/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ fn make_svg_opacity(color: BackendColor) -> String {
enum Target<'a> {
File(String, &'a Path),
Buffer(&'a mut String),
// TODO: At this point we won't make the breaking change
// so the u8 buffer is still supported. But in 0.3, we definitely
// should get rid of this.
#[cfg(feature = "deprecated_items")]
U8Buffer(String, &'a mut Vec<u8>),
}

impl Target<'_> {
fn get_mut(&mut self) -> &mut String {
match self {
Target::File(ref mut buf, _) => buf,
Target::Buffer(buf) => buf,
#[cfg(feature = "deprecated_items")]
Target::U8Buffer(ref mut buf, _) => buf,
}
}
}
Expand Down Expand Up @@ -147,24 +140,6 @@ impl<'a> SVGBackend<'a> {
ret
}

/// Create a new SVG drawing backend and store the document into a u8 vector
#[cfg(feature = "deprecated_items")]
#[deprecated(
note = "This will be replaced by `with_string`, consider use `with_string` to avoid breaking change in the future"
)]
pub fn with_buffer(buf: &'a mut Vec<u8>, size: (u32, u32)) -> Self {
let mut ret = Self {
target: Target::U8Buffer(String::default(), buf),
size,
tag_stack: vec![],
saved: false,
};

ret.init_svg_file(size);

ret
}

/// Create a new SVG drawing backend and store the document into a String buffer
pub fn with_string(buf: &'a mut String, size: (u32, u32)) -> Self {
let mut ret = Self {
Expand Down Expand Up @@ -203,11 +178,6 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
.map_err(DrawingErrorKind::DrawingError)?;
}
Target::Buffer(_) => {}
#[cfg(feature = "deprecated_items")]
Target::U8Buffer(ref actual, ref mut target) => {
target.clear();
target.extend_from_slice(actual.as_bytes());
}
}
self.saved = true;
}
Expand Down Expand Up @@ -481,11 +451,11 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
}

#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
fn blit_bitmap<'b>(
fn blit_bitmap(
&mut self,
pos: BackendCoord,
(w, h): (u32, u32),
src: &'b [u8],
src: &[u8],
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
use image::codecs::png::PngEncoder;
use image::ImageEncoder;
Expand All @@ -508,9 +478,7 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
}

let padding = (3 - data.len() % 3) % 3;
for _ in 0..padding {
data.push(0);
}
data.resize(data.len() + padding, 0);

let mut rem_bits = 0;
let mut rem_num = 0;
Expand Down
5 changes: 5 additions & 0 deletions plotters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.3.6"
authors = ["Hao Hou <haohou302@gmail.com>"]
edition = "2018"
license = "MIT"
msrv = "1.56"
description = "A Rust drawing library focus on data plotting for both WASM and native applications"
repository = "https://github.com/plotters-rs/plotters"
homepage = "https://plotters-rs.github.io/"
Expand All @@ -12,6 +13,10 @@ categories = ["visualization", "wasm"]
readme = "../README.md"
exclude = ["doc-template", "plotters-doc-data"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(doc_cfg)'] }
deprecated = { level = "allow" }

[dependencies]
num-traits = "0.2.14"
chrono = { version = "0.4.32", optional = true }
Expand Down
1 change: 0 additions & 1 deletion plotters/clippy.toml

This file was deleted.

2 changes: 1 addition & 1 deletion plotters/examples/3d-plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let z_axis = (-3.0..3.0).step(0.1);

let mut chart = ChartBuilder::on(&area)
.caption("3D Plot Test".to_string(), ("sans", 20))
.caption("3D Plot Test", ("sans", 20))
.build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;

chart.with_projection(|mut pb| {
Expand Down
2 changes: 1 addition & 1 deletion plotters/examples/matshow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.margin(5)
.top_x_label_area_size(40)
.y_label_area_size(40)
.build_cartesian_2d(0i32..15i32, 15i32..0i32)?;
.build_cartesian_2d(0i32..15i32, 0i32..15i32)?;

chart
.configure_mesh()
Expand Down
10 changes: 5 additions & 5 deletions plotters/src/chart/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
Sets the size of the X label area at the bottom of the chart.
- `size`: The desired size of the X label area in backend units (pixels).
If set to 0, the X label area is removed.
If set to 0, the X label area is removed.
See [`ChartBuilder::on()`] for more information and examples.
*/
Expand All @@ -206,7 +206,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
Sets the size of the Y label area to the left of the chart.
- `size`: The desired size of the Y label area in backend units (pixels).
If set to 0, the Y label area is removed.
If set to 0, the Y label area is removed.
See [`ChartBuilder::on()`] for more information and examples.
*/
Expand All @@ -218,7 +218,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
Sets the size of the X label area at the top of the chart.
- `size`: The desired size of the top X label area in backend units (pixels).
If set to 0, the top X label area is removed.
If set to 0, the top X label area is removed.
See [`ChartBuilder::on()`] for more information and examples.
*/
Expand All @@ -230,7 +230,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
Sets the size of the Y label area to the right of the chart.
- `size`: The desired size of the Y label area in backend units (pixels).
If set to 0, the Y label area to the right is removed.
If set to 0, the Y label area to the right is removed.
See [`ChartBuilder::on()`] for more information and examples.
*/
Expand All @@ -243,7 +243,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
- `pos`: The position of the desired label area to adjust
- `size`: The desired size of the label area in backend units (pixels).
If set to 0, the label area is removed.
If set to 0, the label area is removed.
See [`ChartBuilder::on()`] for more information and examples.
*/
Expand Down
6 changes: 3 additions & 3 deletions plotters/src/chart/context/cartesian3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ where
/// Override the 3D projection matrix. This function allows to override the default projection
/// matrix.
/// - `pf`: A function that takes the default projection matrix configuration and returns the
/// projection matrix. This function will allow you to adjust the pitch, yaw angle and the
/// centeral point of the projection, etc. You can also build a projection matrix which is not
/// relies on the default configuration as well.
/// projection matrix. This function will allow you to adjust the pitch, yaw angle and the
/// centeral point of the projection, etc. You can also build a projection matrix which is not
/// relies on the default configuration as well.
pub fn with_projection<P: FnOnce(ProjectionMatrixBuilder) -> ProjectionMatrix>(
&mut self,
pf: P,
Expand Down
2 changes: 1 addition & 1 deletion plotters/src/coord/ranged1d/combinators/logarithmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<V: LogScalable> LogCoord<V> {
let a = V::from_f64(fv + self.zero_point);
let b = V::from_f64(self.zero_point);

(V::as_f64(&a) - V::as_f64(&b)).abs() < std::f64::EPSILON
(V::as_f64(&a) - V::as_f64(&b)).abs() < f64::EPSILON
}
}

Expand Down
7 changes: 4 additions & 3 deletions plotters/src/coord/ranged1d/discrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ where
/// - Add an extra dummy element after all the values in original discrete coordinate
/// - Logically each value `v` from original coordinate system is mapped into an segment `[v, v+1)` where `v+1` denotes the successor of the `v`
/// - Introduce two types of values `SegmentValue::Exact(value)` which denotes the left end of value's segment and `SegmentValue::CenterOf(value)` which refers the center of the segment.
/// This is used in histogram types, which uses a discrete coordinate as the buckets. The segmented coord always emits `CenterOf(value)` key points, thus it allows all the label and tick marks
/// of the coordinate rendered in the middle of each segment.
/// The corresponding trait [IntoSegmentedCoord](trait.IntoSegmentedCoord.html) is used to apply this decorator to coordinates.
/// This is used in histogram types, which uses a discrete coordinate as the buckets.
/// The segmented coord always emits `CenterOf(value)` key points, thus it allows all the label and tick marks
/// of the coordinate rendered in the middle of each segment.
/// The corresponding trait [IntoSegmentedCoord](trait.IntoSegmentedCoord.html) is used to apply this decorator to coordinates.
#[derive(Clone)]
pub struct SegmentedCoord<D: DiscreteRanged>(D);

Expand Down
8 changes: 4 additions & 4 deletions plotters/src/coord/ranged1d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ pub trait Ranged {
/// This marker decides if Plotters default [ValueFormatter](trait.ValueFormatter.html) implementation should be used.
/// This associated type can be one of the following two types:
/// - [DefaultFormatting](struct.DefaultFormatting.html) will allow Plotters to automatically impl
/// the formatter based on `Debug` trait, if `Debug` trait is not impl for the `Self::Value`,
/// [ValueFormatter](trait.ValueFormatter.html) will not impl unless you impl it manually.
/// the formatter based on `Debug` trait, if `Debug` trait is not impl for the `Self::Value`,
/// [ValueFormatter](trait.ValueFormatter.html) will not impl unless you impl it manually.
///
/// - [NoDefaultFormatting](struct.NoDefaultFormatting.html) Disable the automatic `Debug`
/// based value formatting. Thus you have to impl the
/// [ValueFormatter](trait.ValueFormatter.html) manually.
/// based value formatting. Thus you have to impl the
/// [ValueFormatter](trait.ValueFormatter.html) manually.
///
type FormatOption: DefaultValueFormatOption;

Expand Down
3 changes: 3 additions & 0 deletions plotters/src/coord/ranged1d/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,19 @@ impl<Z: TimeZone> TimeValue for DateTime<Z> {

impl TimeValue for NaiveDateTime {
type DateType = NaiveDate;

fn date_floor(&self) -> NaiveDate {
self.date()
}

fn date_ceil(&self) -> NaiveDate {
if self.time().num_seconds_from_midnight() > 0 {
self.date() + Duration::days(1)
} else {
self.date()
}
}

fn earliest_after_date(date: NaiveDate) -> NaiveDateTime {
date.and_hms(0, 0, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion plotters/src/coord/ranged1d/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use datetime::{
mod numeric;
pub use numeric::{
RangedCoordf32, RangedCoordf64, RangedCoordi128, RangedCoordi32, RangedCoordi64,
RangedCoordu128, RangedCoordu32, RangedCoordu64, RangedCoordusize, RangedCoordisize,
RangedCoordisize, RangedCoordu128, RangedCoordu32, RangedCoordu64, RangedCoordusize,
};

mod slice;
Expand Down
6 changes: 3 additions & 3 deletions plotters/src/coord/ranged1d/types/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ macro_rules! gen_key_points_comp {

assert!(!(range.0.is_nan() || range.1.is_nan()));

if (range.0 - range.1).abs() < std::f64::EPSILON {
if (range.0 - range.1).abs() < f64::EPSILON {
return vec![range.0 as $type];
}

Expand All @@ -141,7 +141,7 @@ macro_rules! gen_key_points_comp {
} else {
a - (a / b).ceil() * b
};
if (ret - b).abs() < std::f64::EPSILON {
if (ret - b).abs() < f64::EPSILON {
0.0
} else {
ret
Expand Down Expand Up @@ -191,7 +191,7 @@ macro_rules! gen_key_points_comp {
let left_base = (left / value_granularity).floor() * value_granularity;
let mut left_relative = left - left_base;
let right = range.1 - rem_euclid(range.1, scale);
while (right - left_relative - left_base) >= -std::f64::EPSILON {
while (right - left_relative - left_base) >= -f64::EPSILON {
let new_left_relative =
(left_relative / value_granularity).round() * value_granularity;
if new_left_relative < 0.0 {
Expand Down
2 changes: 1 addition & 1 deletion plotters/src/data/quartiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Quartiles {
assert!(0_f64 <= pct);
let hundred = 100_f64;
assert!(pct <= hundred);
if (pct - hundred).abs() < std::f64::EPSILON {
if (pct - hundred).abs() < f64::EPSILON {
return s[s.len() - 1].into();
}
let length = (s.len() - 1) as f64;
Expand Down
8 changes: 4 additions & 4 deletions plotters/src/drawing/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ impl<DB: DrawingBackend, CT: CoordTranslate> DrawingArea<DB, CT> {

/// Compute the relative size based on the drawing area's height
pub fn relative_to_height(&self, p: f64) -> f64 {
f64::from((self.rect.y1 - self.rect.y0).max(0)) * (p.min(1.0).max(0.0))
f64::from((self.rect.y1 - self.rect.y0).max(0)) * (p.clamp(0.0, 1.0))
}

/// Compute the relative size based on the drawing area's width
pub fn relative_to_width(&self, p: f64) -> f64 {
f64::from((self.rect.x1 - self.rect.x0).max(0)) * (p.min(1.0).max(0.0))
f64::from((self.rect.x1 - self.rect.x0).max(0)) * (p.clamp(0.0, 1.0))
}

/// Get the pixel range of this area
Expand Down Expand Up @@ -669,13 +669,13 @@ mod drawing_area_tests {
for row in 0..=nyb {
for col in 0..=nxb {
let get_bp = |full, limit, id| {
(if id == 0 {
if id == 0 {
0
} else if id > limit {
full
} else {
breaks[id as usize - 1]
})
}
};

let expected_u = (get_bp(1024, nxb, col), get_bp(768, nyb, row));
Expand Down
6 changes: 3 additions & 3 deletions plotters/src/element/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'a, Coord, P: PixelFormat> BitMapElement<'a, Coord, P> {
/// - `size`: The size of the bitmap
/// - `buf`: The buffer to use
/// - **returns**: The newly created image element, if the buffer isn't fit the image
/// dimension, this will returns an `None`.
/// dimension, this will returns an `None`.
pub fn with_owned_buffer(pos: Coord, size: (u32, u32), buf: Vec<u8>) -> Option<Self> {
if buf.len() < (size.0 * size.1) as usize * P::PIXEL_SIZE {
return None;
Expand All @@ -109,7 +109,7 @@ impl<'a, Coord, P: PixelFormat> BitMapElement<'a, Coord, P> {
/// - `size`: The size of the bitmap
/// - `buf`: The buffer to use
/// - **returns**: The newly created image element, if the buffer isn't fit the image
/// dimension, this will returns an `None`.
/// dimension, this will returns an `None`.
pub fn with_mut(pos: Coord, size: (u32, u32), buf: &'a mut [u8]) -> Option<Self> {
if buf.len() < (size.0 * size.1) as usize * P::PIXEL_SIZE {
return None;
Expand All @@ -130,7 +130,7 @@ impl<'a, Coord, P: PixelFormat> BitMapElement<'a, Coord, P> {
/// - `size`: The size of the bitmap
/// - `buf`: The buffer to use
/// - **returns**: The newly created image element, if the buffer isn't fit the image
/// dimension, this will returns an `None`.
/// dimension, this will returns an `None`.
pub fn with_ref(pos: Coord, size: (u32, u32), buf: &'a [u8]) -> Option<Self> {
if buf.len() < (size.0 * size.1) as usize * P::PIXEL_SIZE {
return None;
Expand Down
1 change: 0 additions & 1 deletion plotters/src/element/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Borrow;
use std::i32;

use super::{Drawable, PointCollection};
use crate::style::{FontDesc, FontResult, LayoutBox, TextStyle};
Expand Down
8 changes: 4 additions & 4 deletions plotters/src/evcxr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ pub fn evcxr_figure<
SVGWrapper(buffer, "".to_string())
}

// An evcxr figure that can save to the local file system and render in a notebook.

/// An evcxr figure that can save to the local file system and render in a notebook.
pub fn evcxr_figure_with_saving<
Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
>(
Expand Down Expand Up @@ -77,8 +76,9 @@ pub fn evcxr_bitmap_figure<
draw: Draw,
) -> SVGWrapper {
const PIXEL_SIZE: usize = 3;
let mut buf = Vec::new();
buf.resize((size.0 as usize) * (size.1 as usize) * PIXEL_SIZE, 0);

let mut buf = vec![0; (size.0 as usize) * (size.1 as usize) * PIXEL_SIZE];

let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
draw(root).expect("Drawing failure");
let mut buffer = "".to_string();
Expand Down
Loading

0 comments on commit 6290f4c

Please sign in to comment.