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:
Daniel Verkamp 2022-04-05 14:30:40 -07:00 committed by Chromeos LUCI
parent 3894aa042a
commit a795bfff65
3 changed files with 26 additions and 22 deletions

View file

@ -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 serde::{Deserialize, Serialize};
use std::{
@ -24,6 +28,11 @@ pub trait AsRawDescriptor {
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 {
/// # Safety
/// 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 {
fn into_raw_descriptor(self) -> RawDescriptor {
let descriptor = self.descriptor;

View file

@ -57,27 +57,7 @@ cfg_if::cfg_if! {
}
pub use crate::descriptor::{
AsRawDescriptor, Descriptor, FromRawDescriptor, IntoRawDescriptor, SafeDescriptor,
AsRawDescriptor, AsRawDescriptors, Descriptor, FromRawDescriptor, IntoRawDescriptor,
SafeDescriptor,
};
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()]
}
}

View file

@ -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
/// we have a unique handle to it.
pub fn validate_raw_fd(raw_fd: RawFd) -> Result<RawFd> {