Skip to content

Commit afb42f1

Browse files
committed
perf: move downcast out of trait
it is only used internally and should not be shared as much. While removing this from the trait is technically breaking its not changing the behaviour and isnt used by ksc so it can be changed relatively easily. This allows it to change the arguments reducing the amount of clones.
1 parent 9b9df77 commit afb42f1

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

src/lib.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ pub trait KStruct: Default {
195195
_parent: Option<SharedType<Output::Parent>>,
196196
) -> KResult<OptRc<Output>> {
197197
let output = OptRc::from(Output::default());
198-
let root = Self::downcast(_root, output.clone(), true);
199-
let parent = Self::downcast(_parent, output.clone(), false);
198+
let root = downcast(_root, &output, true);
199+
let parent = downcast(_parent, &output, false);
200200
Output::read(&output, _io, root, parent)?;
201201
Ok(output)
202202
}
@@ -211,43 +211,39 @@ pub trait KStruct: Default {
211211
let mut output = OptRc::from(Output::default());
212212
init(Rc::get_mut(output.get_mut()).unwrap())?;
213213

214-
let root = Self::downcast(_root, output.clone(), true);
215-
let parent = Self::downcast(_parent, output.clone(), false);
214+
let root = downcast(_root, &output, true);
215+
let parent = downcast(_parent, &output, false);
216216
Output::read(&output, _io, root, parent)?;
217217
Ok(output)
218218
}
219+
}
219220

220-
fn downcast<T, U>(
221-
opt_rc: Option<SharedType<U>>,
222-
fallback: OptRc<T>,
223-
panic: bool,
224-
) -> SharedType<U>
225-
where
226-
T: KStruct + Default + Any,
227-
U: 'static,
228-
{
229-
if let Some(rc) = opt_rc {
230-
rc
221+
fn downcast<T, U>(opt_rc: Option<SharedType<U>>, fallback: &OptRc<T>, panic: bool) -> SharedType<U>
222+
where
223+
T: KStruct + Default + Any,
224+
U: 'static,
225+
{
226+
if let Some(rc) = opt_rc {
227+
rc
228+
} else {
229+
let fallback_any: &dyn Any = &fallback.get();
230+
//println!("`{}` is a '{}' type", type_name_of_val(&t), type_name::<Rc<U>>());
231+
if let Some(as_result) = fallback_any.downcast_ref::<Rc<U>>() {
232+
SharedType::<U>::new(as_result.clone())
231233
} else {
232-
let fallback_any: &dyn Any = &fallback.get();
233-
//println!("`{}` is a '{}' type", type_name_of_val(&t), type_name::<Rc<U>>());
234-
if let Some(as_result) = fallback_any.downcast_ref::<Rc<U>>() {
235-
SharedType::<U>::new(as_result.clone())
236-
} else {
237-
#[allow(clippy::incompatible_msrv)] // behind feature flag
238-
#[allow(clippy::manual_assert)]
239-
if panic {
240-
#[cfg(feature = "type_name_of_val")]
241-
panic!(
242-
"`{}` is not a '{}' type",
243-
std::any::type_name_of_val(&fallback),
244-
type_name::<Rc<U>>()
245-
);
246-
#[cfg(not(feature = "type_name_of_val"))]
247-
panic!("`{:p}` is not a '{}' type", &fallback, type_name::<Rc<U>>());
248-
}
249-
SharedType::<U>::empty()
234+
#[allow(clippy::incompatible_msrv)] // behind feature flag
235+
#[allow(clippy::manual_assert)]
236+
if panic {
237+
#[cfg(feature = "type_name_of_val")]
238+
panic!(
239+
"`{}` is not a '{}' type",
240+
std::any::type_name_of_val(&fallback),
241+
type_name::<Rc<U>>()
242+
);
243+
#[cfg(not(feature = "type_name_of_val"))]
244+
panic!("`{:p}` is not a '{}' type", &fallback, type_name::<Rc<U>>());
250245
}
246+
SharedType::<U>::empty()
251247
}
252248
}
253249
}

0 commit comments

Comments
 (0)