Skip to content

Commit

Permalink
Improve documentation, simplify reference comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryStokes committed Jan 18, 2022
1 parent 44b91d4 commit 937debd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
22 changes: 17 additions & 5 deletions src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
/// except in the case where the value is outside of the axis range
pub struct NormalisedValue(pub f32);

/// A trait for creating an axis scale. It has two functions:
/// `ticks` returns a vector of axis ticks
/// `normalise` normalises the input `value`
/// Specifies a generic scale on which axes and data can be rendered
pub trait AxisScale {
/// Provides the list of [ticks](AxisTick) that should be rendered along the axis
fn ticks(&self) -> Vec<AxisTick>;

/// Normalises a value within the axis scale to a number between 0 and 1,
/// where 0 represents the minimum value of the scale, and 1 the maximum
///
/// For example, for a linear scale between 50 and 100:
/// - normalise(50) -> 0
/// - normalise(60) -> 0.2
/// - normalise(75) -> 0.5
/// - normalise(100) -> 1
fn normalise(&self, value: f32) -> NormalisedValue;
}

/// An axis tick
/// Each tick must have a location and a label.
/// An axis tick, specifying a label to be displayed at some normalised
/// position along the axis
pub struct AxisTick {
/// normalised location between zero and one along the axis specifying
/// the position at which the tick should be rendered
pub location: NormalisedValue,

/// text label that should be rendered alongside the tick
pub label: String,
}
6 changes: 4 additions & 2 deletions src/horizontal_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ impl PartialEq for Props {
&& self.tick_len == other.tick_len
&& self.title == other.title
&& std::ptr::eq(
&*self.scale as *const dyn AxisScale as *const u8,
&*other.scale as *const dyn AxisScale as *const u8,
// test reference equality, avoiding issues with vtables discussed in
// https://github.com/rust-lang/rust/issues/46139
&*self.scale as *const _ as *const u8,
&*other.scale as *const _ as *const u8,
)
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/horizontal_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ impl PartialEq for Props {
&& self.y == other.y
&& self.height == other.height
&& self.width == other.width
// test reference equality, avoiding issues with vtables discussed in
// https://github.com/rust-lang/rust/issues/46139
&& std::ptr::eq(
&*self.horizontal_scale as *const dyn AxisScale as *const u8,
&*other.horizontal_scale as *const dyn AxisScale as *const u8,
&*self.horizontal_scale as *const _ as *const u8,
&*other.horizontal_scale as *const _ as *const u8,
)
&& std::ptr::eq(
&*self.vertical_scale as *const dyn AxisScale as *const u8,
&*other.vertical_scale as *const dyn AxisScale as *const u8,
&*self.vertical_scale as *const _ as *const u8,
&*other.vertical_scale as *const _ as *const u8,
)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/vertical_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ impl PartialEq for Props {
&& self.tick_len == other.tick_len
&& self.title == other.title
&& std::ptr::eq(
&*self.scale as *const dyn AxisScale as *const u8,
&*other.scale as *const dyn AxisScale as *const u8,
// test reference equality, avoiding issues with vtables discussed in
// https://github.com/rust-lang/rust/issues/46139
&*self.scale as *const _ as *const u8,
&*other.scale as *const _ as *const u8,
)
}
}
Expand Down

0 comments on commit 937debd

Please sign in to comment.