| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 |
|
| 213 | use crate::{
|
| 214 | alloc::{box_ext::BoxExt, AllocError, Flags},
|
| 215 | error::{self, Error},
|
| 216 | sync::UniqueArc,
|
| 217 | types::{Opaque, ScopeGuard},
|
| 218 | };
|
| 219 | use alloc::boxed::Box;
|
| 220 | use core::{
|
| 221 | cell::UnsafeCell,
|
| 222 | convert::Infallible,
|
| 223 | marker::PhantomData,
|
| 224 | mem::MaybeUninit,
|
| 225 | num::*,
|
| 226 | pin::Pin,
|
| 227 | ptr::{self, NonNull},
|
| 228 | };
|
| 229 |
|
| 230 | #[doc(hidden)]
|
| 231 | pub mod __internal;
|
| 232 | #[doc(hidden)]
|
| 233 | pub mod macros;
|
| 234 |
|
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | #[macro_export]
|
| 273 | macro_rules! stack_pin_init {
|
| 274 | (let $var:ident $(: $t:ty)? = $val:expr) => {
|
| 275 | let val = $val;
|
| 276 | let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
|
| 277 | let mut $var = match $crate::init::__internal::StackInit::init($var, val) {
|
| 278 | Ok(res) => res,
|
| 279 | Err(x) => {
|
| 280 | let x: ::core::convert::Infallible = x;
|
| 281 | match x {}
|
| 282 | }
|
| 283 | };
|
| 284 | };
|
| 285 | }
|
| 286 |
|
| 287 | /// Initialize and pin a type directly on the stack.
|
| 288 | ///
|
| 289 | /// # Examples
|
| 290 | ///
|
| 291 | /// ```rust,ignore
|
| 292 | /// # #![allow(clippy::disallowed_names)]
|
| 293 | /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
|
| 294 | /// # use macros::pin_data;
|
| 295 | /// # use core::{alloc::AllocError, pin::Pin};
|
| 296 | /// #[pin_data]
|
| 297 | /// struct Foo {
|
| 298 | /// #[pin]
|
| 299 | /// a: Mutex<usize>,
|
| 300 | /// b: Box<Bar>,
|
| 301 | /// }
|
| 302 | ///
|
| 303 | /// struct Bar {
|
| 304 | /// x: u32,
|
| 305 | /// }
|
| 306 | ///
|
| 307 | /// stack_try_pin_init!(let foo: Result<Pin<&mut Foo>, AllocError> = pin_init!(Foo {
|
| 308 | /// a <- new_mutex!(42),
|
| 309 | /// b: Box::new(Bar {
|
| 310 | /// x: 64,
|
| 311 | /// }, GFP_KERNEL)?,
|
| 312 | /// }));
|
| 313 | /// let foo = foo.unwrap();
|
| 314 | /// pr_info!("a: {}", &*foo.a.lock());
|
| 315 | /// ```
|
| 316 | ///
|
| 317 | /// ```rust,ignore
|
| 318 | /// # #![allow(clippy::disallowed_names)]
|
| 319 | /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
|
| 320 | /// # use macros::pin_data;
|
| 321 | /// # use core::{alloc::AllocError, pin::Pin};
|
| 322 | /// #[pin_data]
|
| 323 | /// struct Foo {
|
| 324 | /// #[pin]
|
| 325 | /// a: Mutex<usize>,
|
| 326 | /// b: Box<Bar>,
|
| 327 | /// }
|
| 328 | ///
|
| 329 | /// struct Bar {
|
| 330 | /// x: u32,
|
| 331 | /// }
|
| 332 | ///
|
| 333 | /// stack_try_pin_init!(let foo: Pin<&mut Foo> =? pin_init!(Foo {
|
| 334 | /// a <- new_mutex!(42),
|
| 335 | /// b: Box::new(Bar {
|
| 336 | /// x: 64,
|
| 337 | /// }, GFP_KERNEL)?,
|
| 338 | /// }));
|
| 339 | /// pr_info!("a: {}", &*foo.a.lock());
|
| 340 | /// # Ok::<_, AllocError>(())
|
| 341 | /// ```
|
| 342 | ///
|
| 343 | /// # Syntax
|
| 344 | ///
|
| 345 | /// A normal `let` binding with optional type annotation. The expression is expected to implement
|
| 346 | /// [`PinInit`]/[`Init`]. This macro assigns a result to the given variable, adding a `?` after the
|
| 347 | /// `=` will propagate this error.
|
| 348 | #[macro_export]
|
| 349 | macro_rules! stack_try_pin_init {
|
| 350 | (let $var:ident $(: $t:ty)? = $val:expr) => {
|
| 351 | let val = $val;
|
| 352 | let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
|
| 353 | let mut $var = $crate::init::__internal::StackInit::init($var, val);
|
| 354 | };
|
| 355 | (let $var:ident $(: $t:ty)? =? $val:expr) => {
|
| 356 | let val = $val;
|
| 357 | let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
|
| 358 | let mut $var = $crate::init::__internal::StackInit::init($var, val)?;
|
| 359 | };
|
| 360 | }
|
| 361 |
|
| 362 | /// Construct an in-place, pinned initializer for `struct`s.
|
| 363 | ///
|
| 364 | /// This macro defaults the error to [`Infallible`]. If you need [`Error`], then use
|
| 365 | /// [`try_pin_init!`].
|
| 366 | ///
|
| 367 | /// The syntax is almost identical to that of a normal `struct` initializer:
|
| 368 | ///
|
| 369 | /// ```rust
|
| 370 | /// # #![allow(clippy::disallowed_names)]
|
| 371 | /// # use kernel::{init, pin_init, macros::pin_data, init::*};
|
| 372 | /// # use core::pin::Pin;
|
| 373 | /// #[pin_data]
|
| 374 | /// struct Foo {
|
| 375 | /// a: usize,
|
| 376 | /// b: Bar,
|
| 377 | /// }
|
| 378 | ///
|
| 379 | /// #[pin_data]
|
| 380 | /// struct Bar {
|
| 381 | /// x: u32,
|
| 382 | /// }
|
| 383 | ///
|
| 384 | /// # fn demo() -> impl PinInit<Foo> {
|
| 385 | /// let a = 42;
|
| 386 | ///
|
| 387 | /// let initializer = pin_init!(Foo {
|
| 388 | /// a,
|
| 389 | /// b: Bar {
|
| 390 | /// x: 64,
|
| 391 | /// },
|
| 392 | /// });
|
| 393 | /// # initializer }
|
| 394 | /// # Box::pin_init(demo(), GFP_KERNEL).unwrap();
|
| 395 | /// ```
|
| 396 | ///
|
| 397 | /// Arbitrary Rust expressions can be used to set the value of a variable.
|
| 398 | ///
|
| 399 | /// The fields are initialized in the order that they appear in the initializer. So it is possible
|
| 400 | /// to read already initialized fields using raw pointers.
|
| 401 | ///
|
| 402 | /// IMPORTANT: You are not allowed to create references to fields of the struct inside of the
|
| 403 | /// initializer.
|
| 404 | ///
|
| 405 | /// # Init-functions
|
| 406 | ///
|
| 407 | /// When working with this API it is often desired to let others construct your types without
|
| 408 | /// giving access to all fields. This is where you would normally write a plain function `new`
|
| 409 | /// that would return a new instance of your type. With this API that is also possible.
|
| 410 | /// However, there are a few extra things to keep in mind.
|
| 411 | ///
|
| 412 | /// To create an initializer function, simply declare it like this:
|
| 413 | ///
|
| 414 | /// ```rust
|
| 415 | /// # #![allow(clippy::disallowed_names)]
|
| 416 | /// # use kernel::{init, pin_init, init::*};
|
| 417 | /// # use core::pin::Pin;
|
| 418 | /// # #[pin_data]
|
| 419 | /// # struct Foo {
|
| 420 | /// # a: usize,
|
| 421 | /// # b: Bar,
|
| 422 | /// # }
|
| 423 | /// # #[pin_data]
|
| 424 | /// # struct Bar {
|
| 425 | /// # x: u32,
|
| 426 | /// # }
|
| 427 | /// impl Foo {
|
| 428 | /// fn new() -> impl PinInit<Self> {
|
| 429 | /// pin_init!(Self {
|
| 430 | /// a: 42,
|
| 431 | /// b: Bar {
|
| 432 | /// x: 64,
|
| 433 | /// },
|
| 434 | /// })
|
| 435 | /// }
|
| 436 | /// }
|
| 437 | /// ```
|
| 438 | ///
|
| 439 | /// Users of `Foo` can now create it like this:
|
| 440 | ///
|
| 441 | /// ```rust
|
| 442 | /// # #![allow(clippy::disallowed_names)]
|
| 443 | /// # use kernel::{init, pin_init, macros::pin_data, init::*};
|
| 444 | /// # use core::pin::Pin;
|
| 445 | /// # #[pin_data]
|
| 446 | /// # struct Foo {
|
| 447 | /// # a: usize,
|
| 448 | /// # b: Bar,
|
| 449 | /// # }
|
| 450 | /// # #[pin_data]
|
| 451 | /// # struct Bar {
|
| 452 | /// # x: u32,
|
| 453 | /// # }
|
| 454 | /// # impl Foo {
|
| 455 | /// # fn new() -> impl PinInit<Self> {
|
| 456 | /// # pin_init!(Self {
|
| 457 | /// # a: 42,
|
| 458 | /// # b: Bar {
|
| 459 | /// # x: 64,
|
| 460 | /// # },
|
| 461 | /// # })
|
| 462 | /// # }
|
| 463 | /// # }
|
| 464 | /// let foo = Box::pin_init(Foo::new(), GFP_KERNEL);
|
| 465 | /// ```
|
| 466 | ///
|
| 467 | /// They can also easily embed it into their own `struct`s:
|
| 468 | ///
|
| 469 | /// ```rust
|
| 470 | /// # #![allow(clippy::disallowed_names)]
|
| 471 | /// # use kernel::{init, pin_init, macros::pin_data, init::*};
|
| 472 | /// # use core::pin::Pin;
|
| 473 | /// # #[pin_data]
|
| 474 | /// # struct Foo {
|
| 475 | /// # a: usize,
|
| 476 | /// # b: Bar,
|
| 477 | /// # }
|
| 478 | /// # #[pin_data]
|
| 479 | /// # struct Bar {
|
| 480 | /// # x: u32,
|
| 481 | /// # }
|
| 482 | /// # impl Foo {
|
| 483 | /// # fn new() -> impl PinInit<Self> {
|
| 484 | /// # pin_init!(Self {
|
| 485 | /// # a: 42,
|
| 486 | /// # b: Bar {
|
| 487 | /// # x: 64,
|
| 488 | /// # },
|
| 489 | /// # })
|
| 490 | /// # }
|
| 491 | /// # }
|
| 492 | /// #[pin_data]
|
| 493 | /// struct FooContainer {
|
| 494 | /// #[pin]
|
| 495 | /// foo1: Foo,
|
| 496 | /// #[pin]
|
| 497 | /// foo2: Foo,
|
| 498 | /// other: u32,
|
| 499 | /// }
|
| 500 | ///
|
| 501 | /// impl FooContainer {
|
| 502 | /// fn new(other: u32) -> impl PinInit<Self> {
|
| 503 | /// pin_init!(Self {
|
| 504 | /// foo1 <- Foo::new(),
|
| 505 | /// foo2 <- Foo::new(),
|
| 506 | /// other,
|
| 507 | /// })
|
| 508 | /// }
|
| 509 | /// }
|
| 510 | /// ```
|
| 511 | ///
|
| 512 | /// Here we see that when using `pin_init!` with `PinInit`, one needs to write `<-` instead of `:`.
|
| 513 | /// This signifies that the given field is initialized in-place. As with `struct` initializers, just
|
| 514 | /// writing the field (in this case `other`) without `:` or `<-` means `other: other,`.
|
| 515 | ///
|
| 516 | /// # Syntax
|
| 517 | ///
|
| 518 | /// As already mentioned in the examples above, inside of `pin_init!` a `struct` initializer with
|
| 519 | /// the following modifications is expected:
|
| 520 | /// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
|
| 521 | /// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
|
| 522 | /// pointer named `this` inside of the initializer.
|
| 523 | /// - Using struct update syntax one can place `..Zeroable::zeroed()` at the very end of the
|
| 524 | /// struct, this initializes every field with 0 and then runs all initializers specified in the
|
| 525 | /// body. This can only be done if [`Zeroable`] is implemented for the struct.
|
| 526 | ///
|
| 527 | /// For instance:
|
| 528 | ///
|
| 529 | /// ```rust
|
| 530 | /// # use kernel::{macros::{Zeroable, pin_data}, pin_init};
|
| 531 | /// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
|
| 532 | /// #[pin_data]
|
| 533 | /// #[derive(Zeroable)]
|
| 534 | /// struct Buf {
|
| 535 | /// // `ptr` points into `buf`.
|
| 536 | /// ptr: *mut u8,
|
| 537 | /// buf: [u8; 64],
|
| 538 | /// #[pin]
|
| 539 | /// pin: PhantomPinned,
|
| 540 | /// }
|
| 541 | /// pin_init!(&this in Buf {
|
| 542 | /// buf: [0; 64],
|
| 543 | /// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
|
| 544 | /// pin: PhantomPinned,
|
| 545 | /// });
|
| 546 | /// pin_init!(Buf {
|
| 547 | /// buf: [1; 64],
|
| 548 | /// ..Zeroable::zeroed()
|
| 549 | /// });
|
| 550 | /// ```
|
| 551 | ///
|
| 552 | /// [`try_pin_init!`]: kernel::try_pin_init
|
| 553 | /// [`NonNull<Self>`]: core::ptr::NonNull
|
| 554 | // For a detailed example of how this macro works, see the module documentation of the hidden
|
| 555 | // module `__internal` inside of `init/__internal.rs`.
|
| 556 | #[macro_export]
|
| 557 | macro_rules! pin_init {
|
| 558 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 559 | $($fields:tt)*
|
| 560 | }) => {
|
| 561 | $crate::__init_internal!(
|
| 562 | @this($($this)?),
|
| 563 | @typ($t $(::<$($generics),*>)?),
|
| 564 | @fields($($fields)*),
|
| 565 | @error(::core::convert::Infallible),
|
| 566 | @data(PinData, use_data),
|
| 567 | @has_data(HasPinData, __pin_data),
|
| 568 | @construct_closure(pin_init_from_closure),
|
| 569 | @munch_fields($($fields)*),
|
| 570 | )
|
| 571 | };
|
| 572 | }
|
| 573 |
|
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | #[macro_export]
|
| 614 | macro_rules! try_pin_init {
|
| 615 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 616 | $($fields:tt)*
|
| 617 | }) => {
|
| 618 | $crate::__init_internal!(
|
| 619 | @this($($this)?),
|
| 620 | @typ($t $(::<$($generics),*>)? ),
|
| 621 | @fields($($fields)*),
|
| 622 | @error($crate::error::Error),
|
| 623 | @data(PinData, use_data),
|
| 624 | @has_data(HasPinData, __pin_data),
|
| 625 | @construct_closure(pin_init_from_closure),
|
| 626 | @munch_fields($($fields)*),
|
| 627 | )
|
| 628 | };
|
| 629 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 630 | $($fields:tt)*
|
| 631 | }? $err:ty) => {
|
| 632 | $crate::__init_internal!(
|
| 633 | @this($($this)?),
|
| 634 | @typ($t $(::<$($generics),*>)? ),
|
| 635 | @fields($($fields)*),
|
| 636 | @error($err),
|
| 637 | @data(PinData, use_data),
|
| 638 | @has_data(HasPinData, __pin_data),
|
| 639 | @construct_closure(pin_init_from_closure),
|
| 640 | @munch_fields($($fields)*),
|
| 641 | )
|
| 642 | };
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | #[macro_export]
|
| 663 | macro_rules! init {
|
| 664 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 665 | $($fields:tt)*
|
| 666 | }) => {
|
| 667 | $crate::__init_internal!(
|
| 668 | @this($($this)?),
|
| 669 | @typ($t $(::<$($generics),*>)?),
|
| 670 | @fields($($fields)*),
|
| 671 | @error(::core::convert::Infallible),
|
| 672 | @data(InitData, ),
|
| 673 | @has_data(HasInitData, __init_data),
|
| 674 | @construct_closure(init_from_closure),
|
| 675 | @munch_fields($($fields)*),
|
| 676 | )
|
| 677 | }
|
| 678 | }
|
| 679 |
|
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | #[macro_export]
|
| 714 | macro_rules! try_init {
|
| 715 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 716 | $($fields:tt)*
|
| 717 | }) => {
|
| 718 | $crate::__init_internal!(
|
| 719 | @this($($this)?),
|
| 720 | @typ($t $(::<$($generics),*>)?),
|
| 721 | @fields($($fields)*),
|
| 722 | @error($crate::error::Error),
|
| 723 | @data(InitData, ),
|
| 724 | @has_data(HasInitData, __init_data),
|
| 725 | @construct_closure(init_from_closure),
|
| 726 | @munch_fields($($fields)*),
|
| 727 | )
|
| 728 | };
|
| 729 | ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
|
| 730 | $($fields:tt)*
|
| 731 | }? $err:ty) => {
|
| 732 | $crate::__init_internal!(
|
| 733 | @this($($this)?),
|
| 734 | @typ($t $(::<$($generics),*>)?),
|
| 735 | @fields($($fields)*),
|
| 736 | @error($err),
|
| 737 | @data(InitData, ),
|
| 738 | @has_data(HasInitData, __init_data),
|
| 739 | @construct_closure(init_from_closure),
|
| 740 | @munch_fields($($fields)*),
|
| 741 | )
|
| 742 | };
|
| 743 | }
|
| 744 |
|
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | #[must_use = "An initializer must be used in order to create its value."]
|
| 769 | pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
|
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>;
|
| 779 |
|
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
|
| 820 | where
|
| 821 | F: FnOnce(Pin<&mut T>) -> Result<(), E>,
|
| 822 | {
|
| 823 | ChainPinInit(self, f, PhantomData)
|
| 824 | }
|
| 825 | }
|
| 826 |
|
| 827 | |
| 828 | pub struct ChainPinInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, Box<T>)>);
|
| 829 |
|
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainPinInit<I, F, T, E>
|
| 835 | where
|
| 836 | I: PinInit<T, E>,
|
| 837 | F: FnOnce(Pin<&mut T>) -> Result<(), E>,
|
| 838 | {
|
| 839 | unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
|
| 840 | |
| 841 | unsafe { self.0.__pinned_init(slot)? };
|
| 842 | |
| 843 | let val = unsafe { &mut *slot };
|
| 844 | |
| 845 | let val = unsafe { Pin::new_unchecked(val) };
|
| 846 | |
| 847 | (self.1)(val).inspect_err(|_| unsafe { core::ptr::drop_in_place(slot) })
|
| 848 | }
|
| 849 | }
|
| 850 |
|
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | #[must_use = "An initializer must be used in order to create its value."]
|
| 881 | pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
|
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | unsafe fn __init(self, slot: *mut T) -> Result<(), E>;
|
| 890 |
|
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
|
| 919 | where
|
| 920 | F: FnOnce(&mut T) -> Result<(), E>,
|
| 921 | {
|
| 922 | ChainInit(self, f, PhantomData)
|
| 923 | }
|
| 924 | }
|
| 925 |
|
| 926 | |
| 927 | pub struct ChainInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, Box<T>)>);
|
| 928 |
|
| 929 | |
| 930 | |
| 931 | |
| 932 | unsafe impl<T: ?Sized, E, I, F> Init<T, E> for ChainInit<I, F, T, E>
|
| 933 | where
|
| 934 | I: Init<T, E>,
|
| 935 | F: FnOnce(&mut T) -> Result<(), E>,
|
| 936 | {
|
| 937 | unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
|
| 938 | |
| 939 | unsafe { self.0.__pinned_init(slot)? };
|
| 940 | |
| 941 | (self.1)(unsafe { &mut *slot }).inspect_err(|_|
|
| 942 | |
| 943 | unsafe { core::ptr::drop_in_place(slot) })
|
| 944 | }
|
| 945 | }
|
| 946 |
|
| 947 | |
| 948 | unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainInit<I, F, T, E>
|
| 949 | where
|
| 950 | I: Init<T, E>,
|
| 951 | F: FnOnce(&mut T) -> Result<(), E>,
|
| 952 | {
|
| 953 | unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
|
| 954 | |
| 955 | unsafe { self.__init(slot) }
|
| 956 | }
|
| 957 | }
|
| 958 |
|
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | #[inline]
|
| 972 | pub const unsafe fn pin_init_from_closure<T: ?Sized, E>(
|
| 973 | f: impl FnOnce(*mut T) -> Result<(), E>,
|
| 974 | ) -> impl PinInit<T, E> {
|
| 975 | __internal::InitClosure(f, PhantomData)
|
| 976 | }
|
| 977 |
|
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | #[inline]
|
| 991 | pub const unsafe fn init_from_closure<T: ?Sized, E>(
|
| 992 | f: impl FnOnce(*mut T) -> Result<(), E>,
|
| 993 | ) -> impl Init<T, E> {
|
| 994 | __internal::InitClosure(f, PhantomData)
|
| 995 | }
|
| 996 |
|
| 997 | |
| 998 | |
| 999 | |
| 1000 | #[inline]
|
| 1001 | pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> {
|
| 1002 | |
| 1003 | unsafe { init_from_closure(|_| Ok(())) }
|
| 1004 | }
|
| 1005 |
|
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | pub fn init_array_from_fn<I, const N: usize, T, E>(
|
| 1016 | mut make_init: impl FnMut(usize) -> I,
|
| 1017 | ) -> impl Init<[T; N], E>
|
| 1018 | where
|
| 1019 | I: Init<T, E>,
|
| 1020 | {
|
| 1021 | let init = move |slot: *mut [T; N]| {
|
| 1022 | let slot = slot.cast::<T>();
|
| 1023 | |
| 1024 | |
| 1025 | let mut init_count = ScopeGuard::new_with_data(0, |i| {
|
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(slot, i)) };
|
| 1031 | });
|
| 1032 | for i in 0..N {
|
| 1033 | let init = make_init(i);
|
| 1034 | |
| 1035 | let ptr = unsafe { slot.add(i) };
|
| 1036 | |
| 1037 | |
| 1038 | unsafe { init.__init(ptr) }?;
|
| 1039 | *init_count += 1;
|
| 1040 | }
|
| 1041 | init_count.dismiss();
|
| 1042 | Ok(())
|
| 1043 | };
|
| 1044 | |
| 1045 | |
| 1046 | unsafe { init_from_closure(init) }
|
| 1047 | }
|
| 1048 |
|
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
|
| 1060 | mut make_init: impl FnMut(usize) -> I,
|
| 1061 | ) -> impl PinInit<[T; N], E>
|
| 1062 | where
|
| 1063 | I: PinInit<T, E>,
|
| 1064 | {
|
| 1065 | let init = move |slot: *mut [T; N]| {
|
| 1066 | let slot = slot.cast::<T>();
|
| 1067 | |
| 1068 | |
| 1069 | let mut init_count = ScopeGuard::new_with_data(0, |i| {
|
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(slot, i)) };
|
| 1075 | });
|
| 1076 | for i in 0..N {
|
| 1077 | let init = make_init(i);
|
| 1078 | |
| 1079 | let ptr = unsafe { slot.add(i) };
|
| 1080 | |
| 1081 | |
| 1082 | unsafe { init.__pinned_init(ptr) }?;
|
| 1083 | *init_count += 1;
|
| 1084 | }
|
| 1085 | init_count.dismiss();
|
| 1086 | Ok(())
|
| 1087 | };
|
| 1088 | |
| 1089 | |
| 1090 | unsafe { pin_init_from_closure(init) }
|
| 1091 | }
|
| 1092 |
|
| 1093 | |
| 1094 | unsafe impl<T, E> Init<T, E> for T {
|
| 1095 | unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
|
| 1096 | unsafe { slot.write(self) };
|
| 1097 | Ok(())
|
| 1098 | }
|
| 1099 | }
|
| 1100 |
|
| 1101 | |
| 1102 | unsafe impl<T, E> PinInit<T, E> for T {
|
| 1103 | unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
|
| 1104 | unsafe { self.__init(slot) }
|
| 1105 | }
|
| 1106 | }
|
| 1107 |
|
| 1108 | |
| 1109 | pub trait InPlaceInit<T>: Sized {
|
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
|
| 1115 | where
|
| 1116 | E: From<AllocError>;
|
| 1117 |
|
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Pin<Self>>
|
| 1123 | where
|
| 1124 | Error: From<E>,
|
| 1125 | {
|
| 1126 | |
| 1127 | let init = unsafe {
|
| 1128 | pin_init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
|
| 1129 | };
|
| 1130 | Self::try_pin_init(init, flags)
|
| 1131 | }
|
| 1132 |
|
| 1133 | |
| 1134 | fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
|
| 1135 | where
|
| 1136 | E: From<AllocError>;
|
| 1137 |
|
| 1138 | |
| 1139 | fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
|
| 1140 | where
|
| 1141 | Error: From<E>,
|
| 1142 | {
|
| 1143 | |
| 1144 | let init = unsafe {
|
| 1145 | init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
|
| 1146 | };
|
| 1147 | Self::try_init(init, flags)
|
| 1148 | }
|
| 1149 | }
|
| 1150 |
|
| 1151 | impl<T> InPlaceInit<T> for Box<T> {
|
| 1152 | #[inline]
|
| 1153 | fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
|
| 1154 | where
|
| 1155 | E: From<AllocError>,
|
| 1156 | {
|
| 1157 | let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
|
| 1158 | let slot = this.as_mut_ptr();
|
| 1159 | |
| 1160 | |
| 1161 | unsafe { init.__pinned_init(slot)? };
|
| 1162 | |
| 1163 | Ok(unsafe { this.assume_init() }.into())
|
| 1164 | }
|
| 1165 |
|
| 1166 | #[inline]
|
| 1167 | fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
|
| 1168 | where
|
| 1169 | E: From<AllocError>,
|
| 1170 | {
|
| 1171 | let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
|
| 1172 | let slot = this.as_mut_ptr();
|
| 1173 | |
| 1174 | |
| 1175 | unsafe { init.__init(slot)? };
|
| 1176 | |
| 1177 | Ok(unsafe { this.assume_init() })
|
| 1178 | }
|
| 1179 | }
|
| 1180 |
|
| 1181 | impl<T> InPlaceInit<T> for UniqueArc<T> {
|
| 1182 | #[inline]
|
| 1183 | fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
|
| 1184 | where
|
| 1185 | E: From<AllocError>,
|
| 1186 | {
|
| 1187 | let mut this = UniqueArc::new_uninit(flags)?;
|
| 1188 | let slot = this.as_mut_ptr();
|
| 1189 | |
| 1190 | |
| 1191 | unsafe { init.__pinned_init(slot)? };
|
| 1192 | |
| 1193 | Ok(unsafe { this.assume_init() }.into())
|
| 1194 | }
|
| 1195 |
|
| 1196 | #[inline]
|
| 1197 | fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
|
| 1198 | where
|
| 1199 | E: From<AllocError>,
|
| 1200 | {
|
| 1201 | let mut this = UniqueArc::new_uninit(flags)?;
|
| 1202 | let slot = this.as_mut_ptr();
|
| 1203 | |
| 1204 | |
| 1205 | unsafe { init.__init(slot)? };
|
| 1206 | |
| 1207 | Ok(unsafe { this.assume_init() })
|
| 1208 | }
|
| 1209 | }
|
| 1210 |
|
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | pub unsafe trait PinnedDrop: __internal::HasPinData {
|
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | fn drop(self: Pin<&mut Self>, only_call_from_drop: __internal::OnlyCallFromDrop);
|
| 1248 | }
|
| 1249 |
|
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | pub unsafe trait Zeroable {}
|
| 1261 |
|
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | #[inline]
|
| 1266 | pub fn zeroed<T: Zeroable>() -> impl Init<T> {
|
| 1267 | |
| 1268 | |
| 1269 | unsafe {
|
| 1270 | init_from_closure(|slot: *mut T| {
|
| 1271 | slot.write_bytes(0, 1);
|
| 1272 | Ok(())
|
| 1273 | })
|
| 1274 | }
|
| 1275 | }
|
| 1276 |
|
| 1277 | macro_rules! impl_zeroable {
|
| 1278 | ($($({$($generics:tt)*})? $t:ty, )*) => {
|
| 1279 | $(unsafe impl$($($generics)*)? Zeroable for $t {})*
|
| 1280 | };
|
| 1281 | }
|
| 1282 |
|
| 1283 | impl_zeroable! {
|
| 1284 | |
| 1285 | bool,
|
| 1286 | char,
|
| 1287 | u8, u16, u32, u64, u128, usize,
|
| 1288 | i8, i16, i32, i64, i128, isize,
|
| 1289 | f32, f64,
|
| 1290 |
|
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | {<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, (),
|
| 1300 |
|
| 1301 | |
| 1302 | {<T>} MaybeUninit<T>,
|
| 1303 | |
| 1304 | {<T>} Opaque<T>,
|
| 1305 |
|
| 1306 | |
| 1307 | {<T: ?Sized + Zeroable>} UnsafeCell<T>,
|
| 1308 |
|
| 1309 | |
| 1310 | Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
|
| 1311 | Option<NonZeroU128>, Option<NonZeroUsize>,
|
| 1312 | Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
|
| 1313 | Option<NonZeroI128>, Option<NonZeroIsize>,
|
| 1314 |
|
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | {<T: ?Sized>} Option<NonNull<T>>,
|
| 1319 | {<T: ?Sized>} Option<Box<T>>,
|
| 1320 |
|
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | {<T>} *mut T, {<T>} *const T,
|
| 1329 |
|
| 1330 | |
| 1331 | |
| 1332 | {<T>} *mut [T], {<T>} *const [T], *mut str, *const str,
|
| 1333 |
|
| 1334 | |
| 1335 | {<const N: usize, T: Zeroable>} [T; N], {<T: Zeroable>} Wrapping<T>,
|
| 1336 | }
|
| 1337 |
|
| 1338 | macro_rules! impl_tuple_zeroable {
|
| 1339 | ($(,)?) => {};
|
| 1340 | ($first:ident, $($t:ident),* $(,)?) => {
|
| 1341 | // SAFETY: All elements are zeroable and padding can be zero.
|
| 1342 | unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($first, $($t),*) {}
|
| 1343 | impl_tuple_zeroable!($($t),* ,);
|
| 1344 | }
|
| 1345 | }
|
| 1346 |
|
| 1347 | impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J);
|