@@ -3,16 +3,36 @@ use std::hash::Hash;
3
3
use indexmap:: Equivalent ;
4
4
use serde:: ser:: { Serialize , SerializeMap , Serializer } ;
5
5
6
+ use crate :: Profile ;
7
+
6
8
use super :: category_color:: CategoryColor ;
7
9
use super :: fast_hash_map:: FastIndexSet ;
8
10
11
+ /// Implemented by [`Category`], [`Subcategory`], [`CategoryHandle`] and [`SubcategoryHandle`].
12
+ pub trait IntoSubcategoryHandle {
13
+ /// Returns the corresponding [`SubcategoryHandle`].
14
+ fn into_subcategory_handle ( self , profile : & mut Profile ) -> SubcategoryHandle ;
15
+ }
16
+
17
+ /// A profiling category. Has a name and a color.
18
+ ///
19
+ /// Used to categorize stack frames and markers in the front-end. The category's
20
+ /// color is used in the activity graph and in the call tree, and in a few other places.
9
21
#[ derive( Debug , Clone , Copy , PartialOrd , Ord , PartialEq , Eq , Hash ) ]
10
22
pub struct Category < ' a > ( pub & ' a str , pub CategoryColor ) ;
11
23
12
- /// A profiling category, obtained from [`Profile::handle_for_category`](crate::Profile::handle_for_category).
24
+ impl IntoSubcategoryHandle for Category < ' _ > {
25
+ fn into_subcategory_handle ( self , profile : & mut Profile ) -> SubcategoryHandle {
26
+ let category_handle = profile. handle_for_category ( self ) ;
27
+ category_handle. into ( )
28
+ }
29
+ }
30
+
31
+ /// The handle for a [`Category`], obtained from [`Profile::handle_for_category`](crate::Profile::handle_for_category).
32
+ ///
33
+ /// Storing and reusing the handle avoids repeated lookups and can improve performance.
13
34
///
14
- /// Used to categorize stack frames and markers in the front-end. Every category has a color;
15
- /// the color is used in the activity graph and in the call tree, and in a few other places.
35
+ /// The handle is specific to a [`Profile`] instance and cannot be reused across profiles.
16
36
#[ derive( Debug , Clone , Copy , PartialOrd , Ord , PartialEq , Eq , Hash ) ]
17
37
pub struct CategoryHandle ( pub ( crate ) u16 ) ;
18
38
@@ -21,12 +41,33 @@ impl CategoryHandle {
21
41
pub const OTHER : Self = CategoryHandle ( 0 ) ;
22
42
}
23
43
44
+ impl IntoSubcategoryHandle for CategoryHandle {
45
+ fn into_subcategory_handle ( self , _profile : & mut Profile ) -> SubcategoryHandle {
46
+ self . into ( )
47
+ }
48
+ }
49
+
24
50
impl Serialize for CategoryHandle {
25
51
fn serialize < S : Serializer > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error > {
26
52
self . 0 . serialize ( serializer)
27
53
}
28
54
}
29
55
56
+ /// A named subcategory of a [`Category`], for fine-grained annotation of stack frames.
57
+ ///
58
+ /// If you don't need named subcategories, you can just pass a [`Category`] or a
59
+ /// [`CategoryHandle`] in any place where an [`IntoSubcategoryHandle`] is expected;
60
+ /// this will give you the category's default subcategory.
61
+ pub struct Subcategory < ' a > ( pub Category < ' a > , pub & ' a str ) ;
62
+
63
+ impl IntoSubcategoryHandle for Subcategory < ' _ > {
64
+ fn into_subcategory_handle ( self , profile : & mut Profile ) -> SubcategoryHandle {
65
+ let Subcategory ( category, subcategory_name) = self ;
66
+ let category_handle = profile. handle_for_category ( category) ;
67
+ profile. handle_for_subcategory ( category_handle, subcategory_name)
68
+ }
69
+ }
70
+
30
71
#[ derive( Debug , Clone , Copy , PartialOrd , Ord , PartialEq , Eq , Hash ) ]
31
72
pub struct SubcategoryIndex ( pub u16 ) ;
32
73
@@ -35,16 +76,27 @@ impl SubcategoryIndex {
35
76
pub const OTHER : Self = SubcategoryIndex ( 0 ) ;
36
77
}
37
78
38
- /// A subcategory of a [`CategoryHandle`], used to annotate stack frames.
79
+ /// A handle for a [`Subcategory`], or for the default subcategory of a [`CategoryHandle`].
80
+ ///
81
+ /// Used to annotate stack frames.
39
82
///
40
83
/// Every [`CategoryHandle`] can be turned into a [`SubcategoryHandle`] by calling `.into()` -
41
84
/// this will give you the default subcategory of that category.
42
85
///
43
86
/// Subcategory handles for named subcategories can be obtained from
44
87
/// [`Profile::handle_for_subcategory`](crate::Profile::handle_for_subcategory).
88
+ /// Storing and reusing the handle avoids repeated lookups and can improve performance.
89
+ ///
90
+ /// The handle is specific to a Profile instance and cannot be reused across profiles.
45
91
#[ derive( Debug , Clone , Copy , PartialOrd , Ord , PartialEq , Eq , Hash ) ]
46
92
pub struct SubcategoryHandle ( pub ( crate ) CategoryHandle , pub ( crate ) SubcategoryIndex ) ;
47
93
94
+ impl IntoSubcategoryHandle for SubcategoryHandle {
95
+ fn into_subcategory_handle ( self , _profile : & mut Profile ) -> SubcategoryHandle {
96
+ self
97
+ }
98
+ }
99
+
48
100
impl From < CategoryHandle > for SubcategoryHandle {
49
101
fn from ( category : CategoryHandle ) -> Self {
50
102
SubcategoryHandle ( category, SubcategoryIndex :: OTHER )
0 commit comments