Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Document remaining members of bevy_utils #6897

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

@Carter0 Carter0 Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

I think maybe you can be a little stronger with the language here. I don't see a reason at all for someone to return true if the underlying types are different. Though maybe I am missing a use case for something like that?

Maybe just replace avoid with not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting one. Should 0u64.dyn_eq(0u32) return true? IMO it probably should.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this trait be manually implementable at all? Adding the bound DynEq: Eq should make any impls other than the blanket impl impossible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not introduce a breaking change in a docs PR, and even then it may not be desirable force it to only work with same type to same type equality.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good point!

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