Skip to content

Commit de4043d

Browse files
committed
Try to allow plugin wrapper to be optimized away
1 parent 2c7e7be commit de4043d

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/plugin/mod.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(crate) struct PluginWrapper<'a, M, C> {
8787
impl<'a, M: Clone, C: Clone> Clone for PluginWrapper<'a, M, C> {
8888
fn clone(&self) -> Self {
8989
Self {
90-
inner: UnsafeCell::new(self.inner(|this| this.clone())),
90+
inner: UnsafeCell::new(self.with(|this| this.clone())),
9191
}
9292
}
9393
}
@@ -97,7 +97,7 @@ where
9797
C: PixelColor,
9898
{
9999
fn hash<H: Hasher>(&self, state: &mut H) {
100-
self.inner(|this| this.state.hash(state))
100+
self.with(|this| this.state.hash(state))
101101
}
102102
}
103103

@@ -116,10 +116,21 @@ impl<'a, M, C> PluginWrapper<'a, M, C> {
116116
self.inner.into_inner().plugin
117117
}
118118

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 {
120121
let inner = unsafe {
121122
// 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()
123134
};
124135

125136
cb(inner)
@@ -132,23 +143,23 @@ where
132143
M: private::Plugin<'a, C>,
133144
{
134145
pub fn new_line(&self) {
135-
self.inner(|this| this.plugin.new_line());
146+
self.with_mut(|this| this.plugin.new_line());
136147
}
137148

138149
pub fn set_state(&self, state: ProcessingState) {
139-
self.inner(|this| this.state = state);
150+
self.with_mut(|this| this.state = state);
140151
}
141152

142153
#[inline]
143154
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 {
145156
ProcessingState::Measure => Some(token),
146157
ProcessingState::Render => this.plugin.render_token(token),
147158
})
148159
}
149160

150161
pub fn peek_token(&self, source: &mut Parser<'a, C>) -> Option<Token<'a, C>> {
151-
self.inner(|this| {
162+
self.with_mut(|this| {
152163
if this.peeked_token.is_none() {
153164
this.peeked_token = this.plugin.next_token(|| source.next());
154165
}
@@ -158,11 +169,11 @@ where
158169
}
159170

160171
pub fn consume_peeked_token(&self) {
161-
self.inner(|this| this.peeked_token = None);
172+
self.with_mut(|this| this.peeked_token = None);
162173
}
163174

164175
pub fn consume_partial(&self, len: usize) {
165-
self.inner(|this| {
176+
self.with_mut(|this| {
166177
// Only string-like tokens can be partially consumed.
167178
debug_assert!(matches!(
168179
this.peeked_token,
@@ -196,15 +207,15 @@ where
196207
cursor: &mut Cursor,
197208
props: TextBoxProperties<'_, S>,
198209
) {
199-
self.inner(|this| {
210+
self.with_mut(|this| {
200211
this.peeked_token = None;
201212

202213
this.plugin.on_start_render(cursor, &props);
203214
});
204215
}
205216

206217
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());
208219
}
209220

210221
pub fn post_render<T, D>(
@@ -218,7 +229,7 @@ where
218229
T: TextRenderer<Color = C>,
219230
D: DrawTarget<Color = C>,
220231
{
221-
self.inner(|this| {
232+
self.with_mut(|this| {
222233
this.plugin
223234
.post_render(draw_target, character_style, text, bounds)
224235
})

0 commit comments

Comments
 (0)