mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-24 03:15:53 +00:00
6502729d8c
Some checks are pending
Audit / audit (push) Waiting to run
CI / fmt (push) Waiting to run
CI / typo (push) Waiting to run
CI / ci (macos-latest, stable, bash) (push) Waiting to run
CI / ci (ubuntu-latest, stable, bash) (push) Waiting to run
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Waiting to run
Deny / audit (push) Waiting to run
Doc / build (push) Waiting to run
Doc / deploy (push) Blocked by required conditions
* cargo.toml: add `allow_attributes` lint * fix lints * fixes * fmt * fix docs * fix docs * fix expect msg
85 lines
2.3 KiB
Rust
85 lines
2.3 KiB
Rust
//! Wrapper type for partially-`unsafe` usage of `T: !Send`.
|
|
|
|
//---------------------------------------------------------------------------------------------------- Import
|
|
use std::{
|
|
borrow::Borrow,
|
|
ops::{Deref, DerefMut},
|
|
};
|
|
|
|
use bytemuck::TransparentWrapper;
|
|
|
|
//---------------------------------------------------------------------------------------------------- Aliases
|
|
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, TransparentWrapper)]
|
|
#[repr(transparent)]
|
|
/// A wrapper type that `unsafe`ly implements `Send` for any `T`.
|
|
///
|
|
/// This is a marker/wrapper type that allows wrapping
|
|
/// any type `T` such that it implements `Send`.
|
|
///
|
|
/// This is to be used when `T` is `Send`, but only in certain
|
|
/// situations not provable to the compiler, or is otherwise a
|
|
/// a pain to prove and/or less efficient.
|
|
///
|
|
/// It is up to the users of this type to ensure their
|
|
/// usage of `UnsafeSendable` are actually safe.
|
|
///
|
|
/// Notably, `heed`'s table type uses this inside `service`.
|
|
pub(crate) struct UnsafeSendable<T>(T);
|
|
|
|
#[expect(clippy::non_send_fields_in_send_ty)]
|
|
// SAFETY: Users ensure that their usage of this type is safe.
|
|
unsafe impl<T> Send for UnsafeSendable<T> {}
|
|
|
|
impl<T> UnsafeSendable<T> {
|
|
/// Create a new [`UnsafeSendable`].
|
|
///
|
|
/// # Safety
|
|
/// By constructing this type, you must ensure the usage
|
|
/// of the resulting `Self` is follows all the [`Send`] rules.
|
|
pub(crate) const unsafe fn new(t: T) -> Self {
|
|
Self(t)
|
|
}
|
|
|
|
/// Extract the inner `T`.
|
|
#[expect(dead_code)]
|
|
pub(crate) fn into_inner(self) -> T {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<T> Borrow<T> for UnsafeSendable<T> {
|
|
fn borrow(&self) -> &T {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<T> AsRef<T> for UnsafeSendable<T> {
|
|
fn as_ref(&self) -> &T {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<T> AsMut<T> for UnsafeSendable<T> {
|
|
fn as_mut(&mut self) -> &mut T {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
impl<T> Deref for UnsafeSendable<T> {
|
|
type Target = T;
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<T> DerefMut for UnsafeSendable<T> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------------------- Tests
|
|
#[cfg(test)]
|
|
mod test {
|
|
// use super::*;
|
|
}
|