fidl_next/
owned.rs

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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use core::fmt;
use core::marker::PhantomData;
use core::mem::forget;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;

/// An owned value in borrowed backing memory.
pub struct Owned<'buf, T: ?Sized> {
    ptr: NonNull<T>,
    _phantom: PhantomData<&'buf mut [u8]>,
}

// SAFETY: `Owned` doesn't add any restrictions on sending across thread boundaries, and so is
// `Send` if `T` is `Send`.
unsafe impl<T: Send + ?Sized> Send for Owned<'_, T> {}

// SAFETY: `Owned` doesn't add any interior mutability, so it is `Sync` if `T` is `Sync`.
unsafe impl<T: Sync + ?Sized> Sync for Owned<'_, T> {}

impl<T: ?Sized> Drop for Owned<'_, T> {
    fn drop(&mut self) {
        unsafe {
            self.ptr.as_ptr().drop_in_place();
        }
    }
}

impl<T: ?Sized> Owned<'_, T> {
    /// Returns an `Owned` of the given pointer.
    ///
    /// # Safety
    ///
    /// `new_unchecked` takes ownership of the pointed-to value. It must point
    /// to a valid value that is not aliased.
    pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
        Self { ptr: unsafe { NonNull::new_unchecked(ptr) }, _phantom: PhantomData }
    }

    /// Returns the owned value.
    pub fn take(self) -> T
    where
        T: Sized,
    {
        unsafe { self.into_raw().read() }
    }

    /// Consumes the `Owned`, returning its pointer to the owned value.
    pub fn into_raw(self) -> *mut T {
        let result = self.ptr.as_ptr();
        forget(self);
        result
    }
}

impl<T: ?Sized> Deref for Owned<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        unsafe { self.ptr.as_ref() }
    }
}

impl<T: ?Sized> DerefMut for Owned<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { self.ptr.as_mut() }
    }
}

impl<T: fmt::Debug + ?Sized> fmt::Debug for Owned<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(f)
    }
}