From 672bbc00521897cb8581cb1a180ca9fe19ab7ad1 Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Sat, 6 Mar 2021 02:09:41 +0300 Subject: [PATCH] Add converting from [f64; 2], (f64, f64), [..] for ImPlotRange #15 --- implot-sys/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/implot-sys/src/lib.rs b/implot-sys/src/lib.rs index 4055c20..ecb5db7 100644 --- a/implot-sys/src/lib.rs +++ b/implot-sys/src/lib.rs @@ -6,4 +6,55 @@ #[cfg(test)] use imgui_sys as _; +use std::ops::Range; include!("bindings.rs"); + +impl From> for ImPlotRange { + fn from(from: Range) -> Self { + ImPlotRange { + Min: from.start, + Max: from.end, + } + } +} + +impl From<[f64; 2]> for ImPlotRange { + fn from(from: [f64; 2]) -> Self { + ImPlotRange { + Min: from[0], + Max: from[1], + } + } +} + +impl From<(f64, f64)> for ImPlotRange { + fn from(from: (f64, f64)) -> Self { + ImPlotRange { + Min: from.0, + Max: from.1, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plot_range_from_range() { + let r = 5.0..7.0; + let im_range: ImPlotRange = r.clone().into(); + assert_eq!(im_range.Min, r.start); + assert_eq!(im_range.Max, r.end); + + let arr = [7.0, 8.0]; + let im_range: ImPlotRange = arr.clone().into(); + assert_eq!(im_range.Min, arr[0]); + assert_eq!(im_range.Max, arr[1]); + + let tuple = (12.0, 19.0); + let im_range: ImPlotRange = tuple.clone().into(); + assert_eq!(im_range.Min, tuple.0); + assert_eq!(im_range.Max, tuple.1); + } +}