mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-06 10:32:10 +00:00
base: move descriptor traits and fns out of lib.rs
Common descriptor code can be moved into the appropriate descriptor.rs and platform-specific files instead of living in the top-level base lib.rs file. BUG=None TEST=tools/presubmit TEST=kokoro ci Change-Id: I07b8d822c40b563cffd12c7726a5c126bc1a0e10 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3573363 Reviewed-by: Dennis Kempin <denniskempin@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Noah Gold <nkgold@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
parent
3894aa042a
commit
a795bfff65
3 changed files with 26 additions and 22 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2022 The Chromium OS 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 crate::{PollToken, RawDescriptor};
|
use crate::{PollToken, RawDescriptor};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -24,6 +28,11 @@ pub trait AsRawDescriptor {
|
||||||
fn as_raw_descriptor(&self) -> RawDescriptor;
|
fn as_raw_descriptor(&self) -> RawDescriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A trait similar to `AsRawDescriptor` but supports an arbitrary number of descriptors.
|
||||||
|
pub trait AsRawDescriptors {
|
||||||
|
fn as_raw_descriptors(&self) -> Vec<RawDescriptor>;
|
||||||
|
}
|
||||||
|
|
||||||
pub trait FromRawDescriptor {
|
pub trait FromRawDescriptor {
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// Safe only if the caller ensures nothing has access to the descriptor after passing it to
|
/// Safe only if the caller ensures nothing has access to the descriptor after passing it to
|
||||||
|
@ -37,6 +46,15 @@ impl AsRawDescriptor for SafeDescriptor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> AsRawDescriptors for T
|
||||||
|
where
|
||||||
|
T: AsRawDescriptor,
|
||||||
|
{
|
||||||
|
fn as_raw_descriptors(&self) -> Vec<RawDescriptor> {
|
||||||
|
vec![self.as_raw_descriptor()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IntoRawDescriptor for SafeDescriptor {
|
impl IntoRawDescriptor for SafeDescriptor {
|
||||||
fn into_raw_descriptor(self) -> RawDescriptor {
|
fn into_raw_descriptor(self) -> RawDescriptor {
|
||||||
let descriptor = self.descriptor;
|
let descriptor = self.descriptor;
|
||||||
|
|
|
@ -57,27 +57,7 @@ cfg_if::cfg_if! {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use crate::descriptor::{
|
pub use crate::descriptor::{
|
||||||
AsRawDescriptor, Descriptor, FromRawDescriptor, IntoRawDescriptor, SafeDescriptor,
|
AsRawDescriptor, AsRawDescriptors, Descriptor, FromRawDescriptor, IntoRawDescriptor,
|
||||||
|
SafeDescriptor,
|
||||||
};
|
};
|
||||||
pub use platform::*;
|
pub use platform::*;
|
||||||
|
|
||||||
/// Verifies that |raw_descriptor| is actually owned by this process and duplicates it
|
|
||||||
/// to ensure that we have a unique handle to it.
|
|
||||||
#[cfg(unix)]
|
|
||||||
pub fn validate_raw_descriptor(raw_descriptor: RawDescriptor) -> Result<RawDescriptor> {
|
|
||||||
validate_raw_fd(raw_descriptor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait similar to `AsRawDescriptor` but supports an arbitrary number of descriptors.
|
|
||||||
pub trait AsRawDescriptors {
|
|
||||||
fn as_raw_descriptors(&self) -> Vec<RawDescriptor>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> AsRawDescriptors for T
|
|
||||||
where
|
|
||||||
T: AsRawDescriptor,
|
|
||||||
{
|
|
||||||
fn as_raw_descriptors(&self) -> Vec<RawDescriptor> {
|
|
||||||
vec![self.as_raw_descriptor()]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -484,6 +484,12 @@ impl Drop for UnlinkUnixListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Verifies that |raw_descriptor| is actually owned by this process and duplicates it
|
||||||
|
/// to ensure that we have a unique handle to it.
|
||||||
|
pub fn validate_raw_descriptor(raw_descriptor: RawDescriptor) -> Result<RawDescriptor> {
|
||||||
|
validate_raw_fd(raw_descriptor)
|
||||||
|
}
|
||||||
|
|
||||||
/// Verifies that |raw_fd| is actually owned by this process and duplicates it to ensure that
|
/// Verifies that |raw_fd| is actually owned by this process and duplicates it to ensure that
|
||||||
/// we have a unique handle to it.
|
/// we have a unique handle to it.
|
||||||
pub fn validate_raw_fd(raw_fd: RawFd) -> Result<RawFd> {
|
pub fn validate_raw_fd(raw_fd: RawFd) -> Result<RawFd> {
|
||||||
|
|
Loading…
Reference in a new issue