add to_values

This commit is contained in:
Simon Gardling 2022-05-18 14:31:51 -04:00
parent 001a21f250
commit c753621cf8
2 changed files with 23 additions and 3 deletions

View File

@ -183,6 +183,9 @@ impl<'a> From<&'a [f64]> for SteppedVector<'a> {
/// Implements traits that are useful when dealing with Vectors of egui's `Value`
pub trait EguiHelper {
/// Converts to `egui::plot::Values`
fn to_values(self) -> Values;
/// Converts to `egui::plot::Line`
fn to_line(self) -> Line;
@ -195,13 +198,19 @@ pub trait EguiHelper {
impl EguiHelper for Vec<Value> {
#[inline(always)]
fn to_line(self) -> Line { Line::new(Values::from_values(self)) }
fn to_values(self) -> Values { Values::from_values(self) }
#[inline(always)]
fn to_points(self) -> Points { Points::new(Values::from_values(self)) }
fn to_line(self) -> Line { Line::new(self.to_values()) }
#[inline(always)]
fn to_tuple(self) -> Vec<(f64, f64)> { self.iter().map(|ele| (ele.x, ele.y)).collect() }
fn to_points(self) -> Points { Points::new(self.to_values()) }
#[inline(always)]
fn to_tuple(self) -> Vec<(f64, f64)> {
// self.iter().map(|ele| (ele.x, ele.y)).collect()
unsafe { std::mem::transmute::<Vec<Value>, Vec<(f64, f64)>>(self) }
}
}
/*

View File

@ -115,6 +115,17 @@ fn invalid_hashed_storage() {
assert_eq!(hashed_storage_read("aaaa"), None);
}
#[test]
fn to_values() {
use egui::plot::{Value, Values};
use ytbn_graphing_software::EguiHelper;
let data_raw = vec![(0.0, 1.0), (1.0, 3.0), (2.0, 4.0)];
let data: Vec<Value> = data_raw.iter().map(|(x, y)| Value::new(*x, *y)).collect();
let values: Values = data.clone().to_values();
assert_eq!(*values.get_values(), data);
}
#[test]
fn to_tuple() {
use egui::plot::Value;