@@ -87,7 +87,7 @@ pub(crate) struct PluginWrapper<'a, M, C> {
87
87
impl < ' a , M : Clone , C : Clone > Clone for PluginWrapper < ' a , M , C > {
88
88
fn clone ( & self ) -> Self {
89
89
Self {
90
- inner : UnsafeCell :: new ( self . inner ( |this| this. clone ( ) ) ) ,
90
+ inner : UnsafeCell :: new ( self . with ( |this| this. clone ( ) ) ) ,
91
91
}
92
92
}
93
93
}
97
97
C : PixelColor ,
98
98
{
99
99
fn hash < H : Hasher > ( & self , state : & mut H ) {
100
- self . inner ( |this| this. state . hash ( state) )
100
+ self . with ( |this| this. state . hash ( state) )
101
101
}
102
102
}
103
103
@@ -116,10 +116,21 @@ impl<'a, M, C> PluginWrapper<'a, M, C> {
116
116
self . inner . into_inner ( ) . plugin
117
117
}
118
118
119
- fn inner < R > ( & self , cb : impl FnOnce ( & mut PluginInner < ' a , M , C > ) -> R ) -> R {
119
+ #[ inline( always) ]
120
+ fn with < R > ( & self , cb : impl FnOnce ( & PluginInner < ' a , M , C > ) -> R ) -> R {
120
121
let inner = unsafe {
121
122
// SAFETY: This is safe because we aren't exposing the reference.
122
- core:: ptr:: NonNull :: new_unchecked ( self . inner . get ( ) ) . as_mut ( )
123
+ self . inner . get ( ) . as_ref ( ) . unwrap_unchecked ( )
124
+ } ;
125
+
126
+ cb ( inner)
127
+ }
128
+
129
+ #[ inline( always) ]
130
+ fn with_mut < R > ( & self , cb : impl FnOnce ( & mut PluginInner < ' a , M , C > ) -> R ) -> R {
131
+ let inner = unsafe {
132
+ // SAFETY: This is safe because we aren't exposing the reference.
133
+ self . inner . get ( ) . as_mut ( ) . unwrap_unchecked ( )
123
134
} ;
124
135
125
136
cb ( inner)
@@ -132,23 +143,23 @@ where
132
143
M : private:: Plugin < ' a , C > ,
133
144
{
134
145
pub fn new_line ( & self ) {
135
- self . inner ( |this| this. plugin . new_line ( ) ) ;
146
+ self . with_mut ( |this| this. plugin . new_line ( ) ) ;
136
147
}
137
148
138
149
pub fn set_state ( & self , state : ProcessingState ) {
139
- self . inner ( |this| this. state = state) ;
150
+ self . with_mut ( |this| this. state = state) ;
140
151
}
141
152
142
153
#[ inline]
143
154
pub fn render_token ( & self , token : Token < ' a , C > ) -> Option < Token < ' a , C > > {
144
- self . inner ( |this| match this. state {
155
+ self . with_mut ( |this| match this. state {
145
156
ProcessingState :: Measure => Some ( token) ,
146
157
ProcessingState :: Render => this. plugin . render_token ( token) ,
147
158
} )
148
159
}
149
160
150
161
pub fn peek_token ( & self , source : & mut Parser < ' a , C > ) -> Option < Token < ' a , C > > {
151
- self . inner ( |this| {
162
+ self . with_mut ( |this| {
152
163
if this. peeked_token . is_none ( ) {
153
164
this. peeked_token = this. plugin . next_token ( || source. next ( ) ) ;
154
165
}
@@ -158,11 +169,11 @@ where
158
169
}
159
170
160
171
pub fn consume_peeked_token ( & self ) {
161
- self . inner ( |this| this. peeked_token = None ) ;
172
+ self . with_mut ( |this| this. peeked_token = None ) ;
162
173
}
163
174
164
175
pub fn consume_partial ( & self , len : usize ) {
165
- self . inner ( |this| {
176
+ self . with_mut ( |this| {
166
177
// Only string-like tokens can be partially consumed.
167
178
debug_assert ! ( matches!(
168
179
this. peeked_token,
@@ -196,15 +207,15 @@ where
196
207
cursor : & mut Cursor ,
197
208
props : TextBoxProperties < ' _ , S > ,
198
209
) {
199
- self . inner ( |this| {
210
+ self . with_mut ( |this| {
200
211
this. peeked_token = None ;
201
212
202
213
this. plugin . on_start_render ( cursor, & props) ;
203
214
} ) ;
204
215
}
205
216
206
217
pub fn on_rendering_finished ( & self ) {
207
- self . inner ( |this| this. plugin . on_rendering_finished ( ) ) ;
218
+ self . with_mut ( |this| this. plugin . on_rendering_finished ( ) ) ;
208
219
}
209
220
210
221
pub fn post_render < T , D > (
@@ -218,7 +229,7 @@ where
218
229
T : TextRenderer < Color = C > ,
219
230
D : DrawTarget < Color = C > ,
220
231
{
221
- self . inner ( |this| {
232
+ self . with_mut ( |this| {
222
233
this. plugin
223
234
. post_render ( draw_target, character_style, text, bounds)
224
235
} )
0 commit comments