Skip to content

Commit

Permalink
nativescript::profiler module: modernize doc links, export profile_sig!
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Oct 11, 2021
1 parent 77abb70 commit 9abb46e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
25 changes: 0 additions & 25 deletions gdnative-core/src/nativescript/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,28 +321,3 @@ macro_rules! godot_wrap_method {
)
};
}

/// Convenience macro to create a profiling signature with a given tag.
///
/// The expanded code will panic at runtime if the file name or `tag` contains `::` or
/// any NUL-bytes.
///
/// See `nativescript::profiling::Signature` for more information.
///
/// # Examples
///
/// ```rust
/// # fn main() {
/// use gdnative_core::profile_sig;
/// use gdnative_core::nativescript::profiler::profile;
///
/// let answer = profile(profile_sig!("foo"), || 42);
/// assert_eq!(42, answer);
/// # }
/// ```
#[macro_export]
macro_rules! profile_sig {
($tag:expr) => {
$crate::nativescript::profiling::Signature::new(file!(), line!(), $tag)
};
}
39 changes: 33 additions & 6 deletions gdnative-core/src/nativescript/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::private::try_get_api;
/// identifier of the code, usually be the name of the method. None of the substrings should
/// contain `::`.
///
/// To create a `Signature` in the correct form, see `Signature::new` or `profile_sig!`. To
/// create a `Signature` from an existing `CStr` or `CString`, see `Signature::from_raw` and
/// `Signature::from_raw_owned`.
/// To create a `Signature` in the correct form, see [`Signature::new()`] or [`profile_sig!`]. To
/// create a `Signature` from an existing `CStr` or `CString`, see [`Signature::from_raw()`] and
/// [`Signature::from_raw_owned()`].
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Signature<'a> {
sig: Cow<'a, CStr>,
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<'a> Signature<'a> {
Self::from_raw(sig)
}

/// Create a borrowed version of `self` for repeated use with `add_data` or `profile`.
/// Create a borrowed version of `self` for repeated use with [`add_data()`][Self::add_data()] or [`profile()`][Self::add_data()].
#[inline(always)]
pub fn borrow(&self) -> Signature<'_> {
Signature {
Expand All @@ -64,7 +64,7 @@ impl<'a> Signature<'a> {

/// Add a data point to Godot's built-in profiler using this signature.
///
/// See the free function `gdnative::nativescript::profiling::add_data`.
/// See the free function [`profiler::add_data()`][add_data()].
#[inline]
pub fn add_data(&self, time: Duration) {
add_data(self.borrow(), time)
Expand All @@ -73,7 +73,7 @@ impl<'a> Signature<'a> {
/// Times a closure and adds the measured time to Godot's built-in profiler with this
/// signature, and then returns it's return value.
///
/// See the free function `gdnative::nativescript::profiling::profile`.
/// See the free function [`profiler::profile()`][profile()].
#[inline]
pub fn profile<F, R>(&self, f: F) -> R
where
Expand Down Expand Up @@ -155,3 +155,30 @@ where
add_data(signature, Instant::now() - start);
ret
}

/// Convenience macro to create a profiling signature with a given tag.
///
/// The expanded code will panic at runtime if the file name or `tag` contains `::` or
/// any NUL-bytes.
///
/// See [`Signature`] for more information.
///
/// # Examples
///
/// ```rust
/// # fn main() {
/// use gdnative::nativescript::profiler::{profile, profile_sig};
///
/// let answer = profile(profile_sig!("foo"), || 42);
/// assert_eq!(answer, 42);
/// # }
/// ```
#[macro_export]
macro_rules! _profile_sig {
($tag:expr) => {
$crate::nativescript::profiler::Signature::new(file!(), line!(), $tag)
};
}

// Export macro in this module
pub use _profile_sig as profile_sig;
3 changes: 2 additions & 1 deletion gdnative-derive/src/profiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ pub(crate) fn derive_profiled(

let stmts = std::mem::take(&mut item_fn.block.stmts);
item_fn.block = Box::new(parse_quote!({
::gdnative::nativescript::profiling::profile(::gdnative::profile_sig!(#tag), move || {
::gdnative::nativescript::profiler::profile(
::gdnative::nativescript::profiler::profile_sig!(#tag), move || {
#(#stmts)*
})
}));
Expand Down
9 changes: 7 additions & 2 deletions gdnative/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@
// Items, which are #[doc(hidden)] in their original crate and re-exported with a wildcard, lose
// their hidden status. Re-exporting them manually and hiding the wildcard solves this.
#[doc(inline)]
pub use gdnative_core::{core_types, nativescript, object};

// Make macros available inside crate on top-level, for convenience and to make other macros look nicer
// For the user, they are exported either in prelude (convenience) or fully qualified.
#[deprecated]
pub use gdnative_core::{
core_types, godot_dbg, godot_error, godot_gdnative_init, godot_gdnative_terminate, godot_init,
godot_nativescript_init, godot_print, godot_warn, godot_wrap_method, nativescript, object,
godot_dbg, godot_error, godot_gdnative_init, godot_gdnative_terminate, godot_init,
godot_nativescript_init, godot_print, godot_warn, godot_wrap_method,
};

#[doc(hidden)]
Expand Down

0 comments on commit 9abb46e

Please sign in to comment.