Lines Matching defs:T
45 //! - a memory location that can hold your `struct` (this can be the [stack], an [`Arc<T>`],
46 //! [`Box<T>`] or any other smart pointer that supports this library).
133 //! To declare an init macro/function you just return an [`impl PinInit<T, E>`]:
161 //! [`impl PinInit<T, E>`] directly from a closure. Of course you have to ensure that the closure
165 //! `slot` now contains a valid bit pattern for the type `T`,
254 doc = "[`Arc<T>`]: https://rust.docs.kernel.org/kernel/sync/struct.Arc.html"
258 doc = "[`Box<T>`]: https://rust.docs.kernel.org/kernel/alloc/kbox/struct.Box.html"
260 #![cfg_attr(not(kernel), doc = "[`Arc<T>`]: alloc::alloc::sync::Arc")]
261 #![cfg_attr(not(kernel), doc = "[`Box<T>`]: alloc::alloc::boxed::Box")]
263 //! [`impl PinInit<T, E>`]: crate::PinInit
264 //! [`impl Init<T, E>`]: crate::Init
993 /// struct Foo<T> {
995 /// elem: T,
998 /// impl<T> Foo<T> {
999 /// fn project_this(self: Pin<&mut Self>) -> Pin<&mut T> {
1000 /// assert_pinned!(Foo<T>, elem, T, inline);
1026 /// A pin-initializer for the type `T`.
1028 /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
1029 /// be [`Box<T>`], [`Arc<T>`] or even the stack (see [`stack_pin_init!`]).
1044 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1048 doc = "[`Arc<T>`]: https://rust.docs.kernel.org/kernel/sync/struct.Arc.html"
1052 doc = "[`Box<T>`]: https://rust.docs.kernel.org/kernel/alloc/kbox/struct.Box.html"
1054 #[cfg_attr(not(kernel), doc = "[`Arc<T>`]: alloc::alloc::sync::Arc")]
1055 #[cfg_attr(not(kernel), doc = "[`Box<T>`]: alloc::alloc::boxed::Box")]
1057 pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
1066 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>;
1086 fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
1088 F: FnOnce(Pin<&mut T>) -> Result<(), E>,
1095 pub struct ChainPinInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, T)>);
1101 unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainPinInit<I, F, T, E>
1103 I: PinInit<T, E>,
1104 F: FnOnce(Pin<&mut T>) -> Result<(), E>,
1106 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
1118 /// An initializer for `T`.
1120 /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
1121 /// be [`Box<T>`], [`Arc<T>`] or even the stack (see [`stack_pin_init!`]). Because
1122 /// [`PinInit<T, E>`] is a super trait, you can use every function that takes it as well.
1137 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1142 /// Contrary to its supertype [`PinInit<T, E>`] the caller is allowed to
1147 doc = "[`Arc<T>`]: https://rust.docs.kernel.org/kernel/sync/struct.Arc.html"
1151 doc = "[`Box<T>`]: https://rust.docs.kernel.org/kernel/alloc/kbox/struct.Box.html"
1153 #[cfg_attr(not(kernel), doc = "[`Arc<T>`]: alloc::alloc::sync::Arc")]
1154 #[cfg_attr(not(kernel), doc = "[`Box<T>`]: alloc::alloc::boxed::Box")]
1156 pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
1164 unsafe fn __init(self, slot: *mut T) -> Result<(), E>;
1194 fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
1196 F: FnOnce(&mut T) -> Result<(), E>,
1203 pub struct ChainInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, T)>);
1208 unsafe impl<T: ?Sized, E, I, F> Init<T, E> for ChainInit<I, F, T, E>
1210 I: Init<T, E>,
1211 F: FnOnce(&mut T) -> Result<(), E>,
1213 unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
1224 unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainInit<I, F, T, E>
1226 I: Init<T, E>,
1227 F: FnOnce(&mut T) -> Result<(), E>,
1229 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
1235 /// Creates a new [`PinInit<T, E>`] from the given closure.
1245 /// - may assume that the `slot` does not move if `T: !Unpin`,
1246 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1248 pub const unsafe fn pin_init_from_closure<T: ?Sized, E>(
1249 f: impl FnOnce(*mut T) -> Result<(), E>,
1250 ) -> impl PinInit<T, E> {
1254 /// Creates a new [`Init<T, E>`] from the given closure.
1265 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1267 pub const unsafe fn init_from_closure<T: ?Sized, E>(
1268 f: impl FnOnce(*mut T) -> Result<(), E>,
1269 ) -> impl Init<T, E> {
1277 /// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
1280 pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
1283 let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
1293 /// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
1296 pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
1299 let res = unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) };
1309 pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> {
1324 pub fn init_array_from_fn<I, const N: usize, T, E>(
1326 ) -> impl Init<[T; N], E>
1328 I: Init<T, E>,
1330 let init = move |slot: *mut [T; N]| {
1331 let slot = slot.cast::<T>();
1334 // SAFETY: Since 0 <= `i` < N, it is still in bounds of `[T; N]`.
1367 pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
1369 ) -> impl PinInit<[T; N], E>
1371 I: PinInit<T, E>,
1373 let init = move |slot: *mut [T; N]| {
1374 let slot = slot.cast::<T>();
1377 // SAFETY: Since 0 <= `i` < N, it is still in bounds of `[T; N]`.
1396 unsafe impl<T> Init<T> for T {
1397 unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible> {
1405 // `slot`. Additionally, all pinning invariants of `T` are upheld.
1406 unsafe impl<T> PinInit<T> for T {
1407 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible> {
1415 // - `Ok(())`, `slot` was initialized and all pinned invariants of `T` are upheld.
1417 unsafe impl<T, E> Init<T, E> for Result<T, E> {
1418 unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
1426 // - `Ok(())`, `slot` was initialized and all pinned invariants of `T` are upheld.
1428 unsafe impl<T, E> PinInit<T, E> for Result<T, E> {
1429 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
1437 pub trait InPlaceWrite<T> {
1444 fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E>;
1449 fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>;
1515 /// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
1549 unsafe impl<T: ZeroableOption> Zeroable for Option<T> {}
1551 // SAFETY: `Option<&T>` is part of the option layout optimization guarantee:
1553 unsafe impl<T> ZeroableOption for &T {}
1554 // SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee:
1556 unsafe impl<T> ZeroableOption for &mut T {}
1557 // SAFETY: `Option<NonNull<T>>` is part of the option layout optimization guarantee:
1559 unsafe impl<T> ZeroableOption for NonNull<T> {}
1561 /// Create an initializer for a zeroed `T`.
1565 pub fn init_zeroed<T: Zeroable>() -> impl Init<T> {
1566 // SAFETY: Because `T: Zeroable`, all bytes zero is a valid bit pattern for `T`
1569 init_from_closure(|slot: *mut T| {
1576 /// Create a `T` consisting of all zeroes.
1579 /// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
1596 pub const fn zeroed<T: Zeroable>() -> T {
1597 // SAFETY:By the type invariants of `Zeroable`, all zeroes is a valid bit pattern for `T`.
1624 {<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, (),
1627 {<T>} MaybeUninit<T>,
1629 // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
1630 {<T: ?Sized + Zeroable>} UnsafeCell<T>,
1641 // We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be
1645 // `T: ?Sized where <T as Pointee>::Metadata: Zeroable`
1646 {<T>} *mut T, {<T>} *const T,
1650 {<T>} *mut [T], {<T>} *const [T], *mut str, *const str,
1652 // SAFETY: `T` is `Zeroable`.
1653 {<const N: usize, T: Zeroable>} [T; N], {<T: Zeroable>} Wrapping<T>,
1681 impl_fn_zeroable_option!(["Rust", "C"] { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U });
1708 pub trait Wrapper<T> {
1709 /// Creates an pin-initializer for a [`Self`] containing `T` from the `value_init` initializer.
1710 fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E>;
1713 impl<T> Wrapper<T> for UnsafeCell<T> {
1714 fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
1715 // SAFETY: `UnsafeCell<T>` has a compatible layout to `T`.
1720 impl<T> Wrapper<T> for MaybeUninit<T> {
1721 fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
1722 // SAFETY: `MaybeUninit<T>` has a compatible layout to `T`.
1728 impl<T> Wrapper<T> for core::pin::UnsafePinned<T> {
1729 fn pin_init<E>(init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
1730 // SAFETY: `UnsafePinned<T>` has a compatible layout to `T`.