diff --git a/base/src/descriptor.rs b/base/src/descriptor.rs index 2723d8e3ec..0b840eae91 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; +} + 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 AsRawDescriptors for T +where + T: AsRawDescriptor, +{ + fn as_raw_descriptors(&self) -> Vec { + 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 36472fc344..7de12306e3 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 { - 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; -} - -impl AsRawDescriptors for T -where - T: AsRawDescriptor, -{ - fn as_raw_descriptors(&self) -> Vec { - vec![self.as_raw_descriptor()] - } -} diff --git a/base/src/unix/mod.rs b/base/src/unix/mod.rs index 7bac9b8f9c..16576e0f0e 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 { + 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 {