xref: /aosp_15_r20/external/crosvm/rutabaga_gfx/src/rutabaga_os/descriptor.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use crate::rutabaga_os::OwnedDescriptor;
6 use crate::rutabaga_os::RawDescriptor;
7 
8 /// Trait for forfeiting ownership of the current raw descriptor, and returning the raw descriptor
9 pub trait IntoRawDescriptor {
into_raw_descriptor(self) -> RawDescriptor10     fn into_raw_descriptor(self) -> RawDescriptor;
11 }
12 
13 /// Trait for returning the underlying raw descriptor, without giving up ownership of the
14 /// descriptor.
15 pub trait AsRawDescriptor {
16     /// Returns the underlying raw descriptor.
17     ///
18     /// Since the descriptor is still owned by the provider, callers should not assume that it will
19     /// remain open for longer than the immediate call of this method. In particular, it is a
20     /// dangerous practice to store the result of this method for future use: instead, it should be
21     /// used to e.g. obtain a raw descriptor that is immediately passed to a system call.
22     ///
23     /// If you need to use the descriptor for a longer time (and particularly if you cannot reliably
24     /// track the lifetime of the providing object), you should probably consider using
25     /// `OwnedDescriptor` (possibly along with `IntoRawDescriptor`) to get full ownership
26     /// over a descriptor pointing to the same resource.
as_raw_descriptor(&self) -> RawDescriptor27     fn as_raw_descriptor(&self) -> RawDescriptor;
28 }
29 
30 pub trait FromRawDescriptor {
31     /// # Safety
32     /// Safe only if the caller ensures nothing has access to the descriptor after passing it to
33     /// `from_raw_descriptor`
from_raw_descriptor(descriptor: RawDescriptor) -> Self34     unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self;
35 }
36 
37 impl IntoRawDescriptor for i64 {
into_raw_descriptor(self) -> RawDescriptor38     fn into_raw_descriptor(self) -> RawDescriptor {
39         self as RawDescriptor
40     }
41 }
42 
43 pub trait AsBorrowedDescriptor {
as_borrowed_descriptor(&self) -> &OwnedDescriptor44     fn as_borrowed_descriptor(&self) -> &OwnedDescriptor;
45 }
46