|
6 | 6 | use super::allocator::{KVmalloc, Kmalloc, Vmalloc};
|
7 | 7 | use super::{AllocError, Allocator, Flags};
|
8 | 8 | use core::alloc::Layout;
|
| 9 | +use core::borrow::{Borrow, BorrowMut}; |
9 | 10 | use core::fmt;
|
10 | 11 | use core::marker::PhantomData;
|
11 | 12 | use core::mem::ManuallyDrop;
|
@@ -499,6 +500,62 @@ where
|
499 | 500 | }
|
500 | 501 | }
|
501 | 502 |
|
| 503 | +/// # Examples |
| 504 | +/// |
| 505 | +/// ``` |
| 506 | +/// # use core::borrow::Borrow; |
| 507 | +/// # use kernel::alloc::KBox; |
| 508 | +/// struct Foo<B: Borrow<u32>>(B); |
| 509 | +/// |
| 510 | +/// // Owned instance. |
| 511 | +/// let owned = Foo(1); |
| 512 | +/// |
| 513 | +/// // Owned instance using `KBox`. |
| 514 | +/// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?); |
| 515 | +/// |
| 516 | +/// let i = 1; |
| 517 | +/// // Borrowed from `i`. |
| 518 | +/// let borrowed = Foo(&i); |
| 519 | +/// # Ok::<(), Error>(()) |
| 520 | +/// ``` |
| 521 | +impl<T, A> Borrow<T> for Box<T, A> |
| 522 | +where |
| 523 | + T: ?Sized, |
| 524 | + A: Allocator, |
| 525 | +{ |
| 526 | + fn borrow(&self) -> &T { |
| 527 | + self.deref() |
| 528 | + } |
| 529 | +} |
| 530 | + |
| 531 | +/// # Examples |
| 532 | +/// |
| 533 | +/// ``` |
| 534 | +/// # use core::borrow::BorrowMut; |
| 535 | +/// # use kernel::alloc::KBox; |
| 536 | +/// struct Foo<B: BorrowMut<u32>>(B); |
| 537 | +/// |
| 538 | +/// // Owned instance. |
| 539 | +/// let owned = Foo(1); |
| 540 | +/// |
| 541 | +/// // Owned instance using `KBox`. |
| 542 | +/// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?); |
| 543 | +/// |
| 544 | +/// let mut i = 1; |
| 545 | +/// // Borrowed from `i`. |
| 546 | +/// let borrowed = Foo(&mut i); |
| 547 | +/// # Ok::<(), Error>(()) |
| 548 | +/// ``` |
| 549 | +impl<T, A> BorrowMut<T> for Box<T, A> |
| 550 | +where |
| 551 | + T: ?Sized, |
| 552 | + A: Allocator, |
| 553 | +{ |
| 554 | + fn borrow_mut(&mut self) -> &mut T { |
| 555 | + self.deref_mut() |
| 556 | + } |
| 557 | +} |
| 558 | + |
502 | 559 | impl<T, A> fmt::Display for Box<T, A>
|
503 | 560 | where
|
504 | 561 | T: ?Sized + fmt::Display,
|
|
0 commit comments