 bd489068c6
			
		
	
	
		bd489068c6
		
			
		
	
	
	
	
		
			
			# Objective - Adjust `bevy_utils` to make it `no_std` compatible - Partially replaces #6581 - Contributes to #8161 - Contributes to #6370 ## Solution Added `alloc` and `std` features to `bevy_utils` (`std` is enabled by default), allowing the crate's use in `no_std` contexts. ## Testing - CI passed locally. - Used `bevy_utils` in a `no_std` crate as an experiment and compiled successfully. ## Migration Guide If you were importing `bevy_utils` and setting `default_features` to `false`, but relying on elements which are now gated behind the `std` or `alloc` features, include the relevant feature in your `Cargo.toml`. ## Notes - Bevy already includes a single `no_std` crate, `bevy_ptr`, so there is precedent for this change. - As `bevy_utils` is widely used across the rest of Bevy, further work to make Bevy `no_std` compatible would be blocked on this crate, if such work was to be undertaken. - Most of the changes in this PR are just the removal of an unnecessary call to `to_string()` within unit tests.
		
			
				
	
	
		
			54 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! A reimplementation of the currently unstable [`std::sync::Exclusive`]
 | |
| //!
 | |
| //! [`std::sync::Exclusive`]: https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html
 | |
| 
 | |
| use core::ptr;
 | |
| 
 | |
| /// See [`Exclusive`](https://github.com/rust-lang/rust/issues/98407) for stdlib's upcoming implementation,
 | |
| /// which should replace this one entirely.
 | |
| ///
 | |
| /// Provides a wrapper that allows making any type unconditionally [`Sync`] by only providing mutable access.
 | |
| #[repr(transparent)]
 | |
| pub struct SyncCell<T: ?Sized> {
 | |
|     inner: T,
 | |
| }
 | |
| 
 | |
| impl<T: Sized> SyncCell<T> {
 | |
|     /// Construct a new instance of a `SyncCell` from the given value.
 | |
|     pub fn new(inner: T) -> Self {
 | |
|         Self { inner }
 | |
|     }
 | |
| 
 | |
|     /// Deconstruct this `SyncCell` into its inner value.
 | |
|     pub fn to_inner(Self { inner }: Self) -> T {
 | |
|         inner
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl<T: ?Sized> SyncCell<T> {
 | |
|     /// Get a reference to this `SyncCell`'s inner value.
 | |
|     pub fn get(&mut self) -> &mut T {
 | |
|         &mut self.inner
 | |
|     }
 | |
| 
 | |
|     /// For types that implement [`Sync`], get shared access to this `SyncCell`'s inner value.
 | |
|     pub fn read(&self) -> &T
 | |
|     where
 | |
|         T: Sync,
 | |
|     {
 | |
|         &self.inner
 | |
|     }
 | |
| 
 | |
|     /// Build a mutable reference to a `SyncCell` from a mutable reference
 | |
|     /// to its inner value, to skip constructing with [`new()`](SyncCell::new()).
 | |
|     pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell<T> {
 | |
|         // SAFETY: repr is transparent, so refs have the same layout; and `SyncCell` properties are `&mut`-agnostic
 | |
|         unsafe { &mut *(ptr::from_mut(r) as *mut SyncCell<T>) }
 | |
|     }
 | |
| }
 | |
| 
 | |
| // SAFETY: `Sync` only allows multithreaded access via immutable reference.
 | |
| // As `SyncCell` requires an exclusive reference to access the wrapped value for `!Sync` types,
 | |
| // marking this type as `Sync` does not actually allow unsynchronized access to the inner value.
 | |
| unsafe impl<T: ?Sized> Sync for SyncCell<T> {}
 |