diff options
author | Daniel Verkamp <dverkamp@chromium.org> | 2022-04-05 14:30:40 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-04-11 20:27:09 +0000 |
commit | a795bfff6545b6231c1f50074b72c5e53389fc1c (patch) | |
tree | 08f8386bc62d9896bc66fe6a06754c0c664a354c | |
parent | 3894aa042a5474ae6e3e58a262d92056a9f11883 (diff) | |
download | crosvm-a795bfff6545b6231c1f50074b72c5e53389fc1c.tar.gz |
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>
-rw-r--r-- | base/src/descriptor.rs | 18 | ||||
-rw-r--r-- | base/src/lib.rs | 24 | ||||
-rw-r--r-- | base/src/unix/mod.rs | 6 |
3 files changed, 26 insertions, 22 deletions
diff --git a/base/src/descriptor.rs b/base/src/descriptor.rs index 2723d8e3e..0b840eae9 100644 --- a/base/src/descriptor.rs +++ b/base/src/descriptor.rs @@ -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; diff --git a/base/src/lib.rs b/base/src/lib.rs index 36472fc34..7de12306e 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -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()] - } -} diff --git a/base/src/unix/mod.rs b/base/src/unix/mod.rs index 7bac9b8f9..16576e0f0 100644 --- a/base/src/unix/mod.rs +++ b/base/src/unix/mod.rs @@ -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> { |