Skip to content

Commit

Permalink
Document remaining members of bevy_utils (bevyengine#6897)
Browse files Browse the repository at this point in the history
# Objective
Partially address bevyengine#3492. 

## Solution
Document the remaining undocumented members of `bevy_utils` and set `warn(missing_docs)` on the crate level. Also enabled `clippy::undocumented_unsafe_blocks` as a warning on the crate to keep it in sync with `bevy_ecs`'s warnings.
  • Loading branch information
james7132 authored and alradish committed Jan 22, 2023
1 parent c0a68c0 commit dff6b01
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/bevy_utils/src/futures.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
//! Utilities for working with [`Future`]s.
//!
//! [`Future`]: std::future::Future
use std::{
future::Future,
pin::Pin,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

/// Consumes the future, polls it once, and immediately returns the output
/// or returns `None` if it wasn't ready yet.
///
/// This will cancel the future if it's not ready.
pub fn now_or_never<F: Future>(mut future: F) -> Option<F::Output> {
let noop_waker = noop_waker();
let mut cx = Context::from_waker(&noop_waker);

// Safety: `future` is not moved and the original value is shadowed
// SAFETY: `future` is not moved and the original value is shadowed
let future = unsafe { Pin::new_unchecked(&mut future) };

match future.poll(&mut cx) {
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ use std::{
hash::{Hash, Hasher},
};

/// An object safe version of [`Eq`]. This trait is automatically implemented
/// for any `'static` type that implements `Eq`.
pub trait DynEq: Any {
/// Casts the type to `dyn Any`.
fn as_any(&self) -> &dyn Any;

/// This method tests for `self` and `other` values to be equal.
///
/// Implementers should avoid returning `true` when the underlying types are
/// not the same.
fn dyn_eq(&self, other: &dyn DynEq) -> bool;
}

Expand All @@ -27,9 +34,15 @@ where
}
}

/// An object safe version of [`Hash`]. This trait is automatically implemented
/// for any `'static` type that implements `Hash`.
pub trait DynHash: DynEq {
/// Casts the type to `dyn Any`.
fn as_dyn_eq(&self) -> &dyn DynEq;

/// Feeds this value into the given [`Hasher`].
///
/// [`Hash`]: std::hash::Hasher
fn dyn_hash(&self, state: &mut dyn Hasher);
}

Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//! General utilities for first-party [Bevy] engine crates.
//!
//! [Bevy]: https://bevyengine.org/
#![warn(missing_docs)]
#![warn(clippy::undocumented_unsafe_blocks)]

#[allow(missing_docs)]
pub mod prelude {
pub use crate::default;
}
Expand Down Expand Up @@ -30,12 +38,14 @@ use std::{
pin::Pin,
};

/// An owned and dynamically typed Future used when you can’t statically type your result or need to add some indirection.
#[cfg(not(target_arch = "wasm32"))]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

#[cfg(target_arch = "wasm32")]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;

/// A shortcut alias for [`hashbrown::hash_map::Entry`].
pub type Entry<'a, K, V> = hashbrown::hash_map::Entry<'a, K, V, RandomState>;

/// A hasher builder that will create a fixed hasher.
Expand Down Expand Up @@ -174,6 +184,8 @@ impl BuildHasher for PassHash {
}
}

/// A no-op hash that only works on `u64`s. Will panic if attempting to
/// hash a type containing non-u64 fields.
#[derive(Debug, Default)]
pub struct PassHasher {
hash: u64,
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_utils/src/synccell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! A reimplementation of the currently unstable [`std::sync::Exclusive`]
//!
//! [`std::sync::Exclusive`]: https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html
/// See [`Exclusive`](https://github.com/rust-lang/rust/issues/98407) for stdlib's upcoming implementation,
/// which should replace this one entirely.
///
Expand Down

0 comments on commit dff6b01

Please sign in to comment.