Skip to main content

mesa3d_util/
descriptor.rs

1// Copyright 2025 Google
2// SPDX-License-Identifier: MIT
3
4use crate::OwnedDescriptor;
5use crate::RawDescriptor;
6
7/// Trait for forfeiting ownership of the current raw descriptor, and returning the raw descriptor
8pub trait IntoRawDescriptor {
9    fn into_raw_descriptor(self) -> RawDescriptor;
10}
11
12/// Trait for returning the underlying raw descriptor, without giving up ownership of the
13/// descriptor.
14pub trait AsRawDescriptor {
15    /// Returns the underlying raw descriptor.
16    ///
17    /// Since the descriptor is still owned by the provider, callers should not assume that it will
18    /// remain open for longer than the immediate call of this method. In particular, it is a
19    /// dangerous practice to store the result of this method for future use: instead, it should be
20    /// used to e.g. obtain a raw descriptor that is immediately passed to a system call.
21    ///
22    /// If you need to use the descriptor for a longer time (and particularly if you cannot reliably
23    /// track the lifetime of the providing object), you should probably consider using
24    /// `OwnedDescriptor` (possibly along with `IntoRawDescriptor`) to get full ownership
25    /// over a descriptor pointing to the same resource.
26    fn as_raw_descriptor(&self) -> RawDescriptor;
27}
28
29pub trait FromRawDescriptor {
30    /// # Safety
31    /// Safe only if the caller ensures nothing has access to the descriptor after passing it to
32    /// `from_raw_descriptor`
33    unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self;
34}
35
36impl IntoRawDescriptor for i64 {
37    fn into_raw_descriptor(self) -> RawDescriptor {
38        self as RawDescriptor
39    }
40}
41
42pub trait AsBorrowedDescriptor {
43    fn as_borrowed_descriptor(&self) -> &OwnedDescriptor;
44}