-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow borrowing plot points by adding PlotPoints::Borrowed
#35
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -728,20 +728,20 @@ impl<'a> Plot<'a> { | |
} | ||
|
||
/// Interact with and add items to the plot and finally draw it. | ||
pub fn show<R>( | ||
pub fn show<'b, R>( | ||
self, | ||
ui: &mut Ui, | ||
build_fn: impl FnOnce(&mut PlotUi) -> R + 'a, | ||
build_fn: impl FnOnce(&mut PlotUi<'b>) -> R + 'a, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a tricky one. The following example helped me with figuring out the correct lifetime annotation: trait PlotItem: std::fmt::Debug {}
#[derive(Debug)]
struct Line<'a>(&'a [f64]);
impl<'a> PlotItem for Line<'a> {}
#[derive(Debug)]
struct PlotUi<'a>(Vec<Box<dyn PlotItem + 'a>>);
impl<'a> PlotUi<'a> {
fn line(&mut self, line: Line<'a>) {
self.0.push(Box::new(line));
}
}
fn show<'a>(f: impl FnOnce(&mut PlotUi<'a>)) {
let mut foo = PlotUi(Vec::new());
f(&mut foo);
}
fn main() {
let v = vec![1.0, 2.0];
show(|plot_ui| {
plot_ui.line(Line(&v));
println!("{plot_ui:?}");
});
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like we should add that code as an example or test then 🤔 |
||
) -> PlotResponse<R> { | ||
self.show_dyn(ui, Box::new(build_fn)) | ||
} | ||
|
||
#[allow(clippy::too_many_lines)] // TODO(emilk): shorten this function | ||
#[allow(clippy::type_complexity)] // build_fn | ||
fn show_dyn<R>( | ||
fn show_dyn<'b, R>( | ||
self, | ||
ui: &mut Ui, | ||
build_fn: Box<dyn FnOnce(&mut PlotUi) -> R + 'a>, | ||
build_fn: Box<dyn FnOnce(&mut PlotUi<'b>) -> R + 'a>, | ||
) -> PlotResponse<R> { | ||
let Self { | ||
id_source, | ||
|
@@ -1467,7 +1467,7 @@ pub fn uniform_grid_spacer<'a>(spacer: impl Fn(GridInput) -> [f64; 3] + 'a) -> G | |
// ---------------------------------------------------------------------------- | ||
|
||
struct PreparedPlot<'a> { | ||
items: Vec<Box<dyn PlotItem>>, | ||
items: Vec<Box<dyn PlotItem + 'a>>, | ||
show_x: bool, | ||
show_y: bool, | ||
label_formatter: LabelFormatter<'a>, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update this docstring