Second round of inlining

This commit is contained in:
novacrazy 2023-04-06 21:39:29 -05:00
parent 374f0c4e27
commit 8f5ae0054d
12 changed files with 62 additions and 0 deletions

View file

@ -135,6 +135,7 @@ impl Mountable for ComponentRepr {
};
}
#[inline]
fn get_closing_node(&self) -> web_sys::Node {
self.closing.node.clone()
}
@ -156,11 +157,13 @@ impl IntoView for ComponentRepr {
impl ComponentRepr {
/// Creates a new [`Component`].
#[inline(always)]
pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
Self::new_with_id_concrete(name.into(), HydrationCtx::id())
}
/// Creates a new [`Component`] with the given hydration ID.
#[inline(always)]
pub fn new_with_id(
name: impl Into<Cow<'static, str>>,
id: HydrationKey,

View file

@ -14,6 +14,7 @@ thread_local! {
// Used in template macro
#[doc(hidden)]
#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[inline(always)]
pub fn add_event_helper<E: crate::ev::EventDescriptor + 'static>(
target: &web_sys::Element,
event: E,

View file

@ -97,6 +97,7 @@ impl AnimationFrameRequestHandle {
/// Runs the given function between the next repaint using
/// [`Window.requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).
#[cfg_attr(debug_assertions, instrument(level = "trace", skip_all))]
#[inline(always)]
pub fn request_animation_frame(cb: impl FnOnce() + 'static) {
_ = request_animation_frame_with_handle(cb);
}
@ -105,6 +106,7 @@ pub fn request_animation_frame(cb: impl FnOnce() + 'static) {
/// [`Window.requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame),
/// returning a cancelable handle.
#[cfg_attr(debug_assertions, instrument(level = "trace", skip_all))]
#[inline(always)]
pub fn request_animation_frame_with_handle(
cb: impl FnOnce() + 'static,
) -> Result<AnimationFrameRequestHandle, JsValue> {
@ -144,6 +146,7 @@ impl IdleCallbackHandle {
/// Queues the given function during an idle period using
/// [`Window.requestIdleCallback`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestIdleCallback).
#[cfg_attr(debug_assertions, instrument(level = "trace", skip_all))]
#[inline(always)]
pub fn request_idle_callback(cb: impl Fn() + 'static) {
_ = request_idle_callback_with_handle(cb);
}
@ -152,6 +155,7 @@ pub fn request_idle_callback(cb: impl Fn() + 'static) {
/// [`Window.requestIdleCallback`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestIdleCallback),
/// returning a cancelable handle.
#[cfg_attr(debug_assertions, instrument(level = "trace", skip_all))]
#[inline(always)]
pub fn request_idle_callback_with_handle(
cb: impl Fn() + 'static,
) -> Result<IdleCallbackHandle, JsValue> {
@ -205,6 +209,7 @@ pub fn set_timeout(cb: impl FnOnce() + 'static, duration: Duration) {
debug_assertions,
instrument(level = "trace", skip_all, fields(duration = ?duration))
)]
#[inline(always)]
pub fn set_timeout_with_handle(
cb: impl FnOnce() + 'static,
duration: Duration,
@ -362,6 +367,7 @@ pub fn set_interval(
debug_assertions,
instrument(level = "trace", skip_all, fields(duration = ?duration))
)]
#[inline(always)]
pub fn set_interval_with_handle(
cb: impl Fn() + 'static,
duration: Duration,
@ -401,6 +407,7 @@ pub fn set_interval_with_handle(
debug_assertions,
instrument(level = "trace", skip_all, fields(event_name = %event_name))
)]
#[inline(always)]
pub fn window_event_listener(
event_name: &str,
cb: impl Fn(web_sys::Event) + 'static,

View file

@ -75,6 +75,7 @@ pub trait ElementDescriptor: ElementDescriptorBounds {
fn name(&self) -> Cow<'static, str>;
/// Determines if the tag is void, i.e., `<input>` and `<br>`.
#[inline(always)]
fn is_void(&self) -> bool {
false
}
@ -140,6 +141,7 @@ pub struct AnyElement {
impl std::ops::Deref for AnyElement {
type Target = web_sys::HtmlElement;
#[inline(always)]
fn deref(&self) -> &Self::Target {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
return &self.element;
@ -150,6 +152,7 @@ impl std::ops::Deref for AnyElement {
}
impl std::convert::AsRef<web_sys::HtmlElement> for AnyElement {
#[inline(always)]
fn as_ref(&self) -> &web_sys::HtmlElement {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
return &self.element;
@ -164,11 +167,13 @@ impl ElementDescriptor for AnyElement {
self.name.clone()
}
#[inline(always)]
fn is_void(&self) -> bool {
self.is_void
}
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
#[inline(always)]
fn hydration_id(&self) -> &HydrationKey {
&self.id
}
@ -254,6 +259,7 @@ impl Custom {
impl std::ops::Deref for Custom {
type Target = web_sys::HtmlElement;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.element
}
@ -261,6 +267,7 @@ impl std::ops::Deref for Custom {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
impl std::convert::AsRef<web_sys::HtmlElement> for Custom {
#[inline(always)]
fn as_ref(&self) -> &web_sys::HtmlElement {
&self.element
}
@ -272,6 +279,7 @@ impl ElementDescriptor for Custom {
}
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
#[inline(always)]
fn hydration_id(&self) -> &HydrationKey {
&self.id
}
@ -413,6 +421,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
#[cfg(debug_assertions)]
/// Adds an optional marker indicating the view macro source.
#[inline(always)]
pub fn with_view_marker(mut self, marker: impl Into<String>) -> Self {
self.view_marker = Some(marker.into());
self
@ -471,6 +480,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// Adds an `id` to the element.
#[track_caller]
#[inline(always)]
pub fn id(self, id: impl Into<Cow<'static, str>>) -> Self {
let id = id.into();
@ -497,6 +507,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
}
/// Binds the element reference to [`NodeRef`].
#[inline(always)]
pub fn node_ref(self, node_ref: NodeRef<El>) -> Self
where
Self: Clone,
@ -579,6 +590,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// of `body`.
///
/// This method will always return [`None`] on non-wasm CSR targets.
#[inline(always)]
pub fn is_mounted(&self) -> bool {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
{
@ -596,6 +608,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// Adds an attribute to this element.
#[track_caller]
#[cfg_attr(all(target_arch = "wasm32", feature = "web"), inline(always))]
pub fn attr(
self,
name: impl Into<Cow<'static, str>>,
@ -699,6 +712,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// Adds a list of classes separated by ASCII whitespace to an element.
#[track_caller]
#[inline(always)]
pub fn classes(self, classes: impl Into<Cow<'static, str>>) -> Self {
self.classes_inner(&classes.into())
}
@ -827,6 +841,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// Adds an event listener to this element.
#[track_caller]
#[inline(always)]
pub fn on<E: EventDescriptor + 'static>(
self,
event: E,
@ -930,6 +945,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
/// Be very careful when using this method. Always remember to
/// sanitize the input to avoid a cross-site scripting (XSS)
/// vulnerability.
#[inline(always)]
pub fn inner_html(self, html: impl Into<Cow<'static, str>>) -> Self {
let html = html.into();
@ -953,6 +969,7 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
impl<El: ElementDescriptor> IntoView for HtmlElement<El> {
#[cfg_attr(debug_assertions, instrument(level = "trace", name = "<HtmlElement />", skip_all, fields(tag = %self.element.name())))]
#[cfg_attr(all(target_arch = "wasm32", feature = "web"), inline(always))]
fn into_view(self, _: Scope) -> View {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
{
@ -1019,6 +1036,7 @@ pub fn custom<El: ElementDescriptor>(cx: Scope, el: El) -> HtmlElement<Custom> {
}
/// Creates a text node.
#[inline(always)]
pub fn text(text: impl Into<Cow<'static, str>>) -> Text {
Text::new(text.into())
}
@ -1080,6 +1098,7 @@ macro_rules! generate_html_tags {
impl std::ops::Deref for [<$tag:camel $($trailing_)?>] {
type Target = web_sys::$el_type;
#[inline(always)]
fn deref(&self) -> &Self::Target {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
{
@ -1093,6 +1112,7 @@ macro_rules! generate_html_tags {
}
impl std::convert::AsRef<web_sys::HtmlElement> for [<$tag:camel $($trailing_)?>] {
#[inline(always)]
fn as_ref(&self) -> &web_sys::HtmlElement {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
return &self.element;
@ -1103,11 +1123,13 @@ macro_rules! generate_html_tags {
}
impl ElementDescriptor for [<$tag:camel $($trailing_)?>] {
#[inline(always)]
fn name(&self) -> Cow<'static, str> {
stringify!($tag).into()
}
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
#[inline(always)]
fn hydration_id(&self) -> &HydrationKey {
&self.id
}
@ -1135,6 +1157,7 @@ macro_rules! generate_html_tags {
};
(@void) => {};
(@void void) => {
#[inline(always)]
fn is_void(&self) -> bool {
true
}

View file

@ -652,6 +652,7 @@ impl View {
///
/// This method will attach an event listener to **all** child
/// [`HtmlElement`] children.
#[inline(always)]
pub fn on<E: ev::EventDescriptor + 'static>(
self,
event: E,
@ -919,6 +920,7 @@ macro_rules! impl_into_view_for_tuples {
where
$($ty: IntoView),*
{
#[inline]
fn into_view(self, cx: Scope) -> View {
paste::paste! {
let ($([<$ty:lower>],)*) = self;
@ -993,12 +995,14 @@ impl IntoView for String {
debug_assertions,
instrument(level = "trace", name = "#text", skip_all)
)]
#[inline(always)]
fn into_view(self, _: Scope) -> View {
View::Text(Text::new(self.into()))
}
}
impl IntoView for &'static str {
#[inline(always)]
fn into_view(self, _: Scope) -> View {
View::Text(Text::new(self.into()))
}
@ -1020,6 +1024,7 @@ macro_rules! viewable_primitive {
($($child_type:ty),* $(,)?) => {
$(
impl IntoView for $child_type {
#[inline(always)]
fn into_view(self, _cx: Scope) -> View {
View::Text(Text::new(self.to_string().into()))
}

View file

@ -21,6 +21,7 @@ pub trait IntoClass {
}
impl IntoClass for bool {
#[inline(always)]
fn into_class(self, _cx: Scope) -> Class {
Class::Value(self)
}
@ -30,6 +31,7 @@ impl<T> IntoClass for T
where
T: Fn() -> bool + 'static,
{
#[inline(always)]
fn into_class(self, cx: Scope) -> Class {
let modified_fn = Box::new(self);
Class::Fn(cx, modified_fn)
@ -60,6 +62,7 @@ impl Class {
}
impl<T: IntoClass> IntoClass for (Scope, T) {
#[inline(always)]
fn into_class(self, _: Scope) -> Class {
self.1.into_class(self.0)
}

View file

@ -36,6 +36,7 @@ where
}
impl<T: IntoProperty> IntoProperty for (Scope, T) {
#[inline(always)]
fn into_property(self, _: Scope) -> Property {
self.1.into_property(self.0)
}

View file

@ -35,6 +35,7 @@ use std::cell::Cell;
/// }
/// }
/// ```
#[repr(transparent)]
pub struct NodeRef<T: ElementDescriptor + 'static>(
RwSignal<Option<HtmlElement<T>>>,
);
@ -70,6 +71,7 @@ pub struct NodeRef<T: ElementDescriptor + 'static>(
/// }
/// }
/// ```
#[inline(always)]
pub fn create_node_ref<T: ElementDescriptor + 'static>(
cx: Scope,
) -> NodeRef<T> {
@ -89,6 +91,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
/// Initially, the value will be `None`, but once it is loaded the effect
/// will rerun and its value will be `Some(Element)`.
#[track_caller]
#[inline(always)]
pub fn get(&self) -> Option<HtmlElement<T>>
where
T: Clone,
@ -120,6 +123,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
/// Runs the provided closure when the `NodeRef` has been connected
/// with it's [`HtmlElement`].
#[inline(always)]
pub fn on_load<F>(self, cx: Scope, f: F)
where
T: Clone,
@ -148,18 +152,21 @@ cfg_if::cfg_if! {
impl<T: Clone + ElementDescriptor + 'static> FnOnce<()> for NodeRef<T> {
type Output = Option<HtmlElement<T>>;
#[inline(always)]
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
self.get()
}
}
impl<T: Clone + ElementDescriptor + 'static> FnMut<()> for NodeRef<T> {
#[inline(always)]
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.get()
}
}
impl<T: Clone + ElementDescriptor + Clone + 'static> Fn<()> for NodeRef<T> {
#[inline(always)]
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.get()
}

View file

@ -4,10 +4,12 @@ use std::{any::Any, fmt, rc::Rc};
/// Wrapper for arbitrary data that can be passed through the view.
#[derive(Clone)]
#[repr(transparent)]
pub struct Transparent(Rc<dyn Any>);
impl Transparent {
/// Creates a new wrapper for this data.
#[inline(always)]
pub fn new<T>(value: T) -> Self
where
T: 'static,
@ -16,6 +18,7 @@ impl Transparent {
}
/// Returns some reference to the inner value if it is of type `T`, or `None` if it isn't.
#[inline(always)]
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: 'static,
@ -31,6 +34,7 @@ impl fmt::Debug for Transparent {
}
impl PartialEq for Transparent {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(&self.0, &other.0)
}
@ -39,6 +43,7 @@ impl PartialEq for Transparent {
impl Eq for Transparent {}
impl IntoView for Transparent {
#[inline(always)]
fn into_view(self, _: Scope) -> View {
View::Transparent(self)
}

View file

@ -3,6 +3,7 @@ use crate::TextProp;
/// A collection of additional HTML attributes to be applied to an element,
/// each of which may or may not be reactive.
#[derive(Default, Clone)]
#[repr(transparent)]
pub struct AdditionalAttributes(pub(crate) Vec<(String, TextProp)>);
impl<I, T, U> From<I> for AdditionalAttributes
@ -22,6 +23,7 @@ where
}
/// Iterator over additional HTML attributes.
#[repr(transparent)]
pub struct AdditionalAttributesIter<'a>(
std::slice::Iter<'a, (String, TextProp)>,
);
@ -29,6 +31,7 @@ pub struct AdditionalAttributesIter<'a>(
impl<'a> Iterator for AdditionalAttributesIter<'a> {
type Item = &'a (String, TextProp);
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}

View file

@ -314,6 +314,7 @@ pub fn generate_head_metadata_separated(cx: Scope) -> (String, String) {
pub struct TextProp(Rc<dyn Fn() -> String>);
impl TextProp {
#[inline(always)]
fn get(&self) -> String {
(self.0)()
}
@ -342,6 +343,7 @@ impl<F> From<F> for TextProp
where
F: Fn() -> String + 'static,
{
#[inline(always)]
fn from(s: F) -> Self {
TextProp(Rc::new(s))
}

View file

@ -35,12 +35,14 @@ impl std::fmt::Debug for TitleContext {
}
/// A function that is applied to the text value before setting `document.title`.
#[repr(transparent)]
pub struct Formatter(Box<dyn Fn(String) -> String>);
impl<F> From<F> for Formatter
where
F: Fn(String) -> String + 'static,
{
#[inline(always)]
fn from(f: F) -> Formatter {
Formatter(Box::new(f))
}