From 90163ceb41b73c8b7796a3ab00548f209499b3ca Mon Sep 17 00:00:00 2001 From: Chirantan Ekbote Date: Fri, 19 Feb 2021 18:19:35 +0900 Subject: [PATCH] base: Use trait objects for ioctls Having these function be generic over F: AsRawDescriptor with a &F parameter means that we cannot use trait objects with them because the compiler complains that `dyn AsRawDescriptor` does not have a compile-time size. Instead change the generic parameter to be a trait object. We're already doing this in a few other places and we're about to make a syscall so the cost of dynamic dispatch is probably overshadowed by the cost of syscall itself. BUG=b:180565632 TEST=unit tests Change-Id: I38f696b621411e7cf3e13af71e426865b6509f6f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2706369 Tested-by: Chirantan Ekbote Tested-by: kokoro Auto-Submit: Chirantan Ekbote Commit-Queue: Stephen Barber Commit-Queue: Daniel Verkamp Reviewed-by: Stephen Barber Reviewed-by: kokoro Reviewed-by: Daniel Verkamp --- base/src/ioctl.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/base/src/ioctl.rs b/base/src/ioctl.rs index a50f689d31..307d5d9381 100644 --- a/base/src/ioctl.rs +++ b/base/src/ioctl.rs @@ -11,16 +11,12 @@ pub unsafe fn ioctl(descriptor: &F, nr: IoctlNr) -> c_int { } /// Run an ioctl with a single value argument. -pub unsafe fn ioctl_with_val( - descriptor: &F, - nr: IoctlNr, - arg: c_ulong, -) -> c_int { +pub unsafe fn ioctl_with_val(descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: c_ulong) -> c_int { libc::ioctl(descriptor.as_raw_descriptor(), nr, arg) } /// Run an ioctl with an immutable reference. -pub unsafe fn ioctl_with_ref(descriptor: &F, nr: IoctlNr, arg: &T) -> c_int { +pub unsafe fn ioctl_with_ref(descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: &T) -> c_int { libc::ioctl( descriptor.as_raw_descriptor(), nr, @@ -29,8 +25,8 @@ pub unsafe fn ioctl_with_ref(descriptor: &F, nr: IoctlNr, } /// Run an ioctl with a mutable reference. -pub unsafe fn ioctl_with_mut_ref( - descriptor: &F, +pub unsafe fn ioctl_with_mut_ref( + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: &mut T, ) -> c_int { @@ -42,8 +38,8 @@ pub unsafe fn ioctl_with_mut_ref( } /// Run an ioctl with a raw pointer. -pub unsafe fn ioctl_with_ptr( - descriptor: &F, +pub unsafe fn ioctl_with_ptr( + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: *const T, ) -> c_int { @@ -51,8 +47,8 @@ pub unsafe fn ioctl_with_ptr( } /// Run an ioctl with a mutable raw pointer. -pub unsafe fn ioctl_with_mut_ptr( - descriptor: &F, +pub unsafe fn ioctl_with_mut_ptr( + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: *mut T, ) -> c_int {