@@ -37,22 +37,22 @@ use axis::AxisWidget;
37
37
use items:: { horizontal_line, rulers_color, vertical_line} ;
38
38
use legend:: LegendWidget ;
39
39
40
- type LabelFormatterFn = dyn Fn ( & str , & PlotPoint ) -> String ;
41
- pub type LabelFormatter = Option < Box < LabelFormatterFn > > ;
40
+ type LabelFormatterFn < ' a > = dyn Fn ( & str , & PlotPoint ) -> String + ' a ;
41
+ pub type LabelFormatter < ' a > = Option < Box < LabelFormatterFn < ' a > > > ;
42
42
43
- type GridSpacerFn = dyn Fn ( GridInput ) -> Vec < GridMark > ;
44
- type GridSpacer = Box < GridSpacerFn > ;
43
+ type GridSpacerFn < ' a > = dyn Fn ( GridInput ) -> Vec < GridMark > + ' a ;
44
+ type GridSpacer < ' a > = Box < GridSpacerFn < ' a > > ;
45
45
46
- type CoordinatesFormatterFn = dyn Fn ( & PlotPoint , & PlotBounds ) -> String ;
46
+ type CoordinatesFormatterFn < ' a > = dyn Fn ( & PlotPoint , & PlotBounds ) -> String + ' a ;
47
47
48
48
/// Specifies the coordinates formatting when passed to [`Plot::coordinates_formatter`].
49
- pub struct CoordinatesFormatter {
50
- function : Box < CoordinatesFormatterFn > ,
49
+ pub struct CoordinatesFormatter < ' a > {
50
+ function : Box < CoordinatesFormatterFn < ' a > > ,
51
51
}
52
52
53
- impl CoordinatesFormatter {
53
+ impl < ' a > CoordinatesFormatter < ' a > {
54
54
/// Create a new formatter based on the pointer coordinate and the plot bounds.
55
- pub fn new ( function : impl Fn ( & PlotPoint , & PlotBounds ) -> String + ' static ) -> Self {
55
+ pub fn new ( function : impl Fn ( & PlotPoint , & PlotBounds ) -> String + ' a ) -> Self {
56
56
Self {
57
57
function : Box :: new ( function) ,
58
58
}
@@ -72,7 +72,7 @@ impl CoordinatesFormatter {
72
72
}
73
73
}
74
74
75
- impl Default for CoordinatesFormatter {
75
+ impl Default for CoordinatesFormatter < ' _ > {
76
76
fn default ( ) -> Self {
77
77
Self :: with_decimals ( 3 )
78
78
}
@@ -143,7 +143,7 @@ pub struct PlotResponse<R> {
143
143
/// Plot::new("my_plot").view_aspect(2.0).show(ui, |plot_ui| plot_ui.line(line));
144
144
/// # });
145
145
/// ```
146
- pub struct Plot {
146
+ pub struct Plot < ' a > {
147
147
id_source : Id ,
148
148
id : Option < Id > ,
149
149
@@ -170,24 +170,24 @@ pub struct Plot {
170
170
171
171
show_x : bool ,
172
172
show_y : bool ,
173
- label_formatter : LabelFormatter ,
174
- coordinates_formatter : Option < ( Corner , CoordinatesFormatter ) > ,
175
- x_axes : Vec < AxisHints > , // default x axes
176
- y_axes : Vec < AxisHints > , // default y axes
173
+ label_formatter : LabelFormatter < ' a > ,
174
+ coordinates_formatter : Option < ( Corner , CoordinatesFormatter < ' a > ) > ,
175
+ x_axes : Vec < AxisHints < ' a > > , // default x axes
176
+ y_axes : Vec < AxisHints < ' a > > , // default y axes
177
177
legend_config : Option < Legend > ,
178
178
show_background : bool ,
179
179
show_axes : Vec2b ,
180
180
181
181
show_grid : Vec2b ,
182
182
grid_spacing : Rangef ,
183
- grid_spacers : [ GridSpacer ; 2 ] ,
183
+ grid_spacers : [ GridSpacer < ' a > ; 2 ] ,
184
184
sharp_grid_lines : bool ,
185
185
clamp_grid : bool ,
186
186
187
187
sense : Sense ,
188
188
}
189
189
190
- impl Plot {
190
+ impl < ' a > Plot < ' a > {
191
191
/// Give a unique id for each plot within the same [`Ui`].
192
192
pub fn new ( id_source : impl std:: hash:: Hash ) -> Self {
193
193
Self {
@@ -405,7 +405,7 @@ impl Plot {
405
405
/// ```
406
406
pub fn label_formatter (
407
407
mut self ,
408
- label_formatter : impl Fn ( & str , & PlotPoint ) -> String + ' static ,
408
+ label_formatter : impl Fn ( & str , & PlotPoint ) -> String + ' a ,
409
409
) -> Self {
410
410
self . label_formatter = Some ( Box :: new ( label_formatter) ) ;
411
411
self
@@ -415,7 +415,7 @@ impl Plot {
415
415
pub fn coordinates_formatter (
416
416
mut self ,
417
417
position : Corner ,
418
- formatter : CoordinatesFormatter ,
418
+ formatter : CoordinatesFormatter < ' a > ,
419
419
) -> Self {
420
420
self . coordinates_formatter = Some ( ( position, formatter) ) ;
421
421
self
@@ -452,7 +452,7 @@ impl Plot {
452
452
///
453
453
/// There are helpers for common cases, see [`log_grid_spacer`] and [`uniform_grid_spacer`].
454
454
#[ inline]
455
- pub fn x_grid_spacer ( mut self , spacer : impl Fn ( GridInput ) -> Vec < GridMark > + ' static ) -> Self {
455
+ pub fn x_grid_spacer ( mut self , spacer : impl Fn ( GridInput ) -> Vec < GridMark > + ' a ) -> Self {
456
456
self . grid_spacers [ 0 ] = Box :: new ( spacer) ;
457
457
self
458
458
}
@@ -461,7 +461,7 @@ impl Plot {
461
461
///
462
462
/// See [`Self::x_grid_spacer`] for explanation.
463
463
#[ inline]
464
- pub fn y_grid_spacer ( mut self , spacer : impl Fn ( GridInput ) -> Vec < GridMark > + ' static ) -> Self {
464
+ pub fn y_grid_spacer ( mut self , spacer : impl Fn ( GridInput ) -> Vec < GridMark > + ' a ) -> Self {
465
465
self . grid_spacers [ 1 ] = Box :: new ( spacer) ;
466
466
self
467
467
}
@@ -662,7 +662,7 @@ impl Plot {
662
662
/// * currently shown range on this axis.
663
663
pub fn x_axis_formatter (
664
664
mut self ,
665
- fmt : impl Fn ( GridMark , usize , & RangeInclusive < f64 > ) -> String + ' static ,
665
+ fmt : impl Fn ( GridMark , usize , & RangeInclusive < f64 > ) -> String + ' a ,
666
666
) -> Self {
667
667
if let Some ( main) = self . x_axes . first_mut ( ) {
668
668
main. formatter = Arc :: new ( fmt) ;
@@ -678,7 +678,7 @@ impl Plot {
678
678
/// * currently shown range on this axis.
679
679
pub fn y_axis_formatter (
680
680
mut self ,
681
- fmt : impl Fn ( GridMark , usize , & RangeInclusive < f64 > ) -> String + ' static ,
681
+ fmt : impl Fn ( GridMark , usize , & RangeInclusive < f64 > ) -> String + ' a ,
682
682
) -> Self {
683
683
if let Some ( main) = self . y_axes . first_mut ( ) {
684
684
main. formatter = Arc :: new ( fmt) ;
@@ -703,7 +703,7 @@ impl Plot {
703
703
///
704
704
/// More than one axis may be specified. The first specified axis is considered the main axis.
705
705
#[ inline]
706
- pub fn custom_x_axes ( mut self , hints : Vec < AxisHints > ) -> Self {
706
+ pub fn custom_x_axes ( mut self , hints : Vec < AxisHints < ' a > > ) -> Self {
707
707
self . x_axes = hints;
708
708
self
709
709
}
@@ -712,17 +712,21 @@ impl Plot {
712
712
///
713
713
/// More than one axis may be specified. The first specified axis is considered the main axis.
714
714
#[ inline]
715
- pub fn custom_y_axes ( mut self , hints : Vec < AxisHints > ) -> Self {
715
+ pub fn custom_y_axes ( mut self , hints : Vec < AxisHints < ' a > > ) -> Self {
716
716
self . y_axes = hints;
717
717
self
718
718
}
719
719
720
720
/// Interact with and add items to the plot and finally draw it.
721
- pub fn show < R > ( self , ui : & mut Ui , build_fn : impl FnOnce ( & mut PlotUi ) -> R ) -> PlotResponse < R > {
721
+ pub fn show < R > (
722
+ self ,
723
+ ui : & mut Ui ,
724
+ build_fn : impl FnOnce ( & mut PlotUi ) -> R + ' a ,
725
+ ) -> PlotResponse < R > {
722
726
self . show_dyn ( ui, Box :: new ( build_fn) )
723
727
}
724
728
725
- fn show_dyn < ' a , R > (
729
+ fn show_dyn < R > (
726
730
self ,
727
731
ui : & mut Ui ,
728
732
build_fn : Box < dyn FnOnce ( & mut PlotUi ) -> R + ' a > ,
@@ -1246,12 +1250,12 @@ impl Plot {
1246
1250
}
1247
1251
1248
1252
/// Returns the rect left after adding axes.
1249
- fn axis_widgets (
1253
+ fn axis_widgets < ' a > (
1250
1254
mem : Option < & PlotMemory > ,
1251
1255
show_axes : Vec2b ,
1252
1256
complete_rect : Rect ,
1253
- [ x_axes, y_axes] : [ & [ AxisHints ] ; 2 ] ,
1254
- ) -> ( [ Vec < AxisWidget > ; 2 ] , Rect ) {
1257
+ [ x_axes, y_axes] : [ & ' a [ AxisHints < ' a > ] ; 2 ] ,
1258
+ ) -> ( [ Vec < AxisWidget < ' a > > ; 2 ] , Rect ) {
1255
1259
// Next we want to create this layout.
1256
1260
// Indices are only examples.
1257
1261
//
@@ -1275,8 +1279,8 @@ fn axis_widgets(
1275
1279
// + +--------------------+---+
1276
1280
//
1277
1281
1278
- let mut x_axis_widgets = Vec :: < AxisWidget > :: new ( ) ;
1279
- let mut y_axis_widgets = Vec :: < AxisWidget > :: new ( ) ;
1282
+ let mut x_axis_widgets = Vec :: < AxisWidget < ' _ > > :: new ( ) ;
1283
+ let mut y_axis_widgets = Vec :: < AxisWidget < ' _ > > :: new ( ) ;
1280
1284
1281
1285
// Will shrink as we add more axes.
1282
1286
let mut rect_left = complete_rect;
@@ -1404,7 +1408,7 @@ pub struct GridMark {
1404
1408
///
1405
1409
/// The logarithmic base, expressing how many times each grid unit is subdivided.
1406
1410
/// 10 is a typical value, others are possible though.
1407
- pub fn log_grid_spacer ( log_base : i64 ) -> GridSpacer {
1411
+ pub fn log_grid_spacer ( log_base : i64 ) -> GridSpacer < ' static > {
1408
1412
let log_base = log_base as f64 ;
1409
1413
let step_sizes = move |input : GridInput | -> Vec < GridMark > {
1410
1414
// handle degenerate cases
@@ -1435,7 +1439,7 @@ pub fn log_grid_spacer(log_base: i64) -> GridSpacer {
1435
1439
///
1436
1440
/// Why only 3 step sizes? Three is the number of different line thicknesses that egui typically uses in the grid.
1437
1441
/// Ideally, those 3 are not hardcoded values, but depend on the visible range (accessible through `GridInput`).
1438
- pub fn uniform_grid_spacer ( spacer : impl Fn ( GridInput ) -> [ f64 ; 3 ] + ' static ) -> GridSpacer {
1442
+ pub fn uniform_grid_spacer < ' a > ( spacer : impl Fn ( GridInput ) -> [ f64 ; 3 ] + ' a ) -> GridSpacer < ' a > {
1439
1443
let get_marks = move |input : GridInput | -> Vec < GridMark > {
1440
1444
let bounds = input. bounds ;
1441
1445
let step_sizes = spacer ( input) ;
@@ -1447,17 +1451,17 @@ pub fn uniform_grid_spacer(spacer: impl Fn(GridInput) -> [f64; 3] + 'static) ->
1447
1451
1448
1452
// ----------------------------------------------------------------------------
1449
1453
1450
- struct PreparedPlot {
1454
+ struct PreparedPlot < ' a > {
1451
1455
items : Vec < Box < dyn PlotItem > > ,
1452
1456
show_x : bool ,
1453
1457
show_y : bool ,
1454
- label_formatter : LabelFormatter ,
1455
- coordinates_formatter : Option < ( Corner , CoordinatesFormatter ) > ,
1458
+ label_formatter : LabelFormatter < ' a > ,
1459
+ coordinates_formatter : Option < ( Corner , CoordinatesFormatter < ' a > ) > ,
1456
1460
// axis_formatters: [AxisFormatter; 2],
1457
1461
transform : PlotTransform ,
1458
1462
show_grid : Vec2b ,
1459
1463
grid_spacing : Rangef ,
1460
- grid_spacers : [ GridSpacer ; 2 ] ,
1464
+ grid_spacers : [ GridSpacer < ' a > ; 2 ] ,
1461
1465
draw_cursor_x : bool ,
1462
1466
draw_cursor_y : bool ,
1463
1467
draw_cursors : Vec < Cursor > ,
@@ -1466,7 +1470,7 @@ struct PreparedPlot {
1466
1470
clamp_grid : bool ,
1467
1471
}
1468
1472
1469
- impl PreparedPlot {
1473
+ impl < ' a > PreparedPlot < ' a > {
1470
1474
fn ui ( self , ui : & mut Ui , response : & Response ) -> ( Vec < Cursor > , Option < Id > ) {
1471
1475
let mut axes_shapes = Vec :: new ( ) ;
1472
1476
0 commit comments