diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-03-08 16:56:14 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-04-08 02:51:37 -0700 |
commit | fdac5ede468e0fddfe527d6108430ee932b02fc3 (patch) | |
tree | 398c2ace79eea2babb4439810c43b793068fd8cc /devices | |
parent | 98895ac05d42ed346a161035134600b0d0e0bb87 (diff) | |
download | crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar.gz crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar.bz2 crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar.lz crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar.xz crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.tar.zst crosvm-fdac5ede468e0fddfe527d6108430ee932b02fc3.zip |
edition: Use dyn syntax for trait objects
Found by running: `cargo rustc -- -D bare_trait_objects` Bare trait objects like `&Trait` and `Box<Trait>` are soft-deprecated in 2018 edition and will start warning at some point. As part of this, I replaced `Box<Trait + 'static>` with `Box<dyn Trait>` because the 'static bound is implied for boxed trait objects. TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu TEST=local kokoro Change-Id: I41c4f13530bece8a34a8ed1c1afd7035b8f86f19 Reviewed-on: https://chromium-review.googlesource.com/1513059 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
Diffstat (limited to 'devices')
30 files changed, 94 insertions, 90 deletions
diff --git a/devices/src/bus.rs b/devices/src/bus.rs index 1b3481b..d4b46eb 100644 --- a/devices/src/bus.rs +++ b/devices/src/bus.rs @@ -106,7 +106,7 @@ impl PartialOrd for BusRange { /// only restriction is that no two devices can overlap in this address space. #[derive(Clone)] pub struct Bus { - devices: BTreeMap<BusRange, Arc<Mutex<BusDevice>>>, + devices: BTreeMap<BusRange, Arc<Mutex<dyn BusDevice>>>, } impl Bus { @@ -117,7 +117,7 @@ impl Bus { } } - fn first_before(&self, addr: u64) -> Option<(BusRange, &Mutex<BusDevice>)> { + fn first_before(&self, addr: u64) -> Option<(BusRange, &Mutex<dyn BusDevice>)> { let (range, dev) = self .devices .range( @@ -132,7 +132,7 @@ impl Bus { Some((*range, dev)) } - fn get_device(&self, addr: u64) -> Option<(u64, &Mutex<BusDevice>)> { + fn get_device(&self, addr: u64) -> Option<(u64, &Mutex<dyn BusDevice>)> { if let Some((range, dev)) = self.first_before(addr) { let offset = addr - range.base; if offset < range.len { @@ -149,7 +149,7 @@ impl Bus { /// Puts the given device at the given address space. pub fn insert( &mut self, - device: Arc<Mutex<BusDevice>>, + device: Arc<Mutex<dyn BusDevice>>, base: u64, len: u64, full_addr: bool, diff --git a/devices/src/pci/ac97.rs b/devices/src/pci/ac97.rs index e58618d..13cb47c 100644 --- a/devices/src/pci/ac97.rs +++ b/devices/src/pci/ac97.rs @@ -38,7 +38,7 @@ pub struct Ac97Dev { impl Ac97Dev { /// Creates an 'Ac97Dev' that uses the given `GuestMemory` and starts with all registers at /// default values. - pub fn new(mem: GuestMemory, audio_server: Box<StreamSource>) -> Self { + pub fn new(mem: GuestMemory, audio_server: Box<dyn StreamSource>) -> Self { let config_regs = PciConfiguration::new( 0x8086, PCI_DEVICE_ID_INTEL_82801AA_5, diff --git a/devices/src/pci/ac97_bus_master.rs b/devices/src/pci/ac97_bus_master.rs index 7d169a3..5f7f444 100644 --- a/devices/src/pci/ac97_bus_master.rs +++ b/devices/src/pci/ac97_bus_master.rs @@ -74,7 +74,7 @@ enum PlaybackError { // Failure reading samples from guest memory. ReadingGuestSamples(data_model::VolatileMemoryError), // Failure to get an buffer from the stream. - StreamError(Box<Error>), + StreamError(Box<dyn Error>), // Failure writing to the audio output. WritingOutput(std::io::Error), } @@ -406,7 +406,7 @@ impl Ac97BusMaster { self.regs.lock().glob_cnt = new_glob_cnt; } - fn start_audio(&mut self, func: Ac97Function, mixer: &Ac97Mixer) -> Result<(), Box<Error>> { + fn start_audio(&mut self, func: Ac97Function, mixer: &Ac97Mixer) -> Result<(), Box<dyn Error>> { const AUDIO_THREAD_RTPRIO: u16 = 12; // Matches other cros audio clients. match func { diff --git a/devices/src/pci/pci_configuration.rs b/devices/src/pci/pci_configuration.rs index a8f9b65..f04284a 100644 --- a/devices/src/pci/pci_configuration.rs +++ b/devices/src/pci/pci_configuration.rs @@ -239,8 +239,8 @@ impl PciConfiguration { vendor_id: u16, device_id: u16, class_code: PciClassCode, - subclass: &PciSubclass, - programming_interface: Option<&PciProgrammingInterface>, + subclass: &dyn PciSubclass, + programming_interface: Option<&dyn PciProgrammingInterface>, header_type: PciHeaderType, subsystem_vendor_id: u16, subsystem_id: u16, @@ -422,7 +422,7 @@ impl PciConfiguration { /// `cap_data` should include the two-byte PCI capability header (type, next), /// but not populate it. Correct values will be generated automatically based /// on `cap_data.id()`. - pub fn add_capability(&mut self, cap_data: &PciCapability) -> Result<usize> { + pub fn add_capability(&mut self, cap_data: &dyn PciCapability) -> Result<usize> { let total_len = cap_data.bytes().len(); // Check that the length is valid. if cap_data.bytes().is_empty() { diff --git a/devices/src/pci/pci_root.rs b/devices/src/pci/pci_root.rs index 781fbd6..d0435a0 100644 --- a/devices/src/pci/pci_root.rs +++ b/devices/src/pci/pci_root.rs @@ -44,7 +44,7 @@ pub struct PciRoot { /// Bus configuration for the root device. root_configuration: PciRootConfiguration, /// Devices attached to this bridge. - devices: Vec<Arc<Mutex<BusDevice>>>, + devices: Vec<Arc<Mutex<dyn BusDevice>>>, } impl PciRoot { @@ -68,7 +68,7 @@ impl PciRoot { } /// Add a `device` to this root PCI bus. - pub fn add_device(&mut self, device: Arc<Mutex<BusDevice>>) { + pub fn add_device(&mut self, device: Arc<Mutex<dyn BusDevice>>) { self.devices.push(device); } diff --git a/devices/src/proxy.rs b/devices/src/proxy.rs index 18d0be0..9880f14 100644 --- a/devices/src/proxy.rs +++ b/devices/src/proxy.rs @@ -66,7 +66,7 @@ enum CommandResult { ReadConfigResult(u32), } -fn child_proc(sock: UnixSeqpacket, device: &mut BusDevice) { +fn child_proc(sock: UnixSeqpacket, device: &mut dyn BusDevice) { let mut running = true; let sock = MsgSocket::<CommandResult, Command>::new(sock); diff --git a/devices/src/register_space/register.rs b/devices/src/register_space/register.rs index 2cc5b30..19e5267 100644 --- a/devices/src/register_space/register.rs +++ b/devices/src/register_space/register.rs @@ -197,7 +197,7 @@ pub struct RegisterSpec<T> { struct RegisterInner<T: RegisterValue> { spec: RegisterSpec<T>, value: T, - write_cb: Option<Box<Fn(T) -> T + Send>>, + write_cb: Option<Box<dyn Fn(T) -> T + Send>>, } /// Register is a thread safe struct. It can be safely changed from any thread. diff --git a/devices/src/register_space/register_space.rs b/devices/src/register_space/register_space.rs index acb0df9..f010be0 100644 --- a/devices/src/register_space/register_space.rs +++ b/devices/src/register_space/register_space.rs @@ -7,7 +7,7 @@ use std::collections::btree_map::BTreeMap; /// Register space repesents a set of registers. It can handle read/write operations. pub struct RegisterSpace { - regs: BTreeMap<RegisterRange, Box<RegisterInterface>>, + regs: BTreeMap<RegisterRange, Box<dyn RegisterInterface>>, } impl RegisterSpace { @@ -70,17 +70,17 @@ impl RegisterSpace { } /// Get first register before this addr. - fn first_before(&self, addr: RegisterOffset) -> Option<&Box<RegisterInterface>> { + fn first_before(&self, addr: RegisterOffset) -> Option<&dyn RegisterInterface> { for (range, r) in self.regs.iter().rev() { if range.from <= addr { - return Some(r); + return Some(r.as_ref()); } } None } /// Get register at this addr. - fn get_register(&self, addr: RegisterOffset) -> Option<&Box<RegisterInterface>> { + fn get_register(&self, addr: RegisterOffset) -> Option<&dyn RegisterInterface> { let r = self.first_before(addr)?; let range = r.range(); if addr <= range.to { diff --git a/devices/src/serial.rs b/devices/src/serial.rs index 6afcf52..078c91b 100644 --- a/devices/src/serial.rs +++ b/devices/src/serial.rs @@ -59,11 +59,11 @@ pub struct Serial { scratch: u8, baud_divisor: u16, in_buffer: VecDeque<u8>, - out: Option<Box<io::Write + Send>>, + out: Option<Box<dyn io::Write + Send>>, } impl Serial { - fn new(interrupt_evt: EventFd, out: Option<Box<io::Write + Send>>) -> Serial { + fn new(interrupt_evt: EventFd, out: Option<Box<dyn io::Write + Send>>) -> Serial { Serial { interrupt_enable: 0, interrupt_identification: DEFAULT_INTERRUPT_IDENTIFICATION, @@ -80,7 +80,7 @@ impl Serial { } /// Constructs a Serial port ready for output. - pub fn new_out(interrupt_evt: EventFd, out: Box<io::Write + Send>) -> Serial { + pub fn new_out(interrupt_evt: EventFd, out: Box<dyn io::Write + Send>) -> Serial { Self::new(interrupt_evt, Some(out)) } diff --git a/devices/src/usb/host_backend/context.rs b/devices/src/usb/host_backend/context.rs index d4c60e3..dc35a9f 100644 --- a/devices/src/usb/host_backend/context.rs +++ b/devices/src/usb/host_backend/context.rs @@ -17,7 +17,7 @@ use vm_control::MaybeOwnedFd; pub struct Context { context: LibUsbContext, event_loop: Arc<EventLoop>, - event_handler: Arc<EventHandler>, + event_handler: Arc<dyn EventHandler>, } impl Context { @@ -124,7 +124,7 @@ impl EventHandler for LibUsbEventHandler { struct PollfdChangeHandler { event_loop: Arc<EventLoop>, - event_handler: Weak<EventHandler>, + event_handler: Weak<dyn EventHandler>, } impl LibUsbPollfdChangeHandler for PollfdChangeHandler { diff --git a/devices/src/usb/host_backend/host_backend_device_provider.rs b/devices/src/usb/host_backend/host_backend_device_provider.rs index 32c5e9a..6aca696 100644 --- a/devices/src/usb/host_backend/host_backend_device_provider.rs +++ b/devices/src/usb/host_backend/host_backend_device_provider.rs @@ -55,7 +55,7 @@ impl HostBackendDeviceProvider { fn start_helper( &mut self, - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, event_loop: Arc<EventLoop>, hub: Arc<UsbHub>, ) -> Result<()> { @@ -67,7 +67,7 @@ impl HostBackendDeviceProvider { let job_queue = AsyncJobQueue::init(&event_loop).map_err(Error::StartAsyncJobQueue)?; let inner = Arc::new(ProviderInner::new(fail_handle, job_queue, ctx, sock, hub)); - let handler: Arc<EventHandler> = inner.clone(); + let handler: Arc<dyn EventHandler> = inner.clone(); event_loop .add_event( &inner.sock, @@ -93,7 +93,7 @@ impl HostBackendDeviceProvider { impl XhciBackendDeviceProvider for HostBackendDeviceProvider { fn start( &mut self, - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, event_loop: Arc<EventLoop>, hub: Arc<UsbHub>, ) -> std::result::Result<(), ()> { @@ -119,7 +119,7 @@ impl XhciBackendDeviceProvider for HostBackendDeviceProvider { /// ProviderInner listens to control socket. pub struct ProviderInner { - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: Arc<AsyncJobQueue>, ctx: Context, sock: MsgSocket<UsbControlResult, UsbControlCommand>, @@ -128,7 +128,7 @@ pub struct ProviderInner { impl ProviderInner { fn new( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: Arc<AsyncJobQueue>, ctx: Context, sock: MsgSocket<UsbControlResult, UsbControlCommand>, diff --git a/devices/src/usb/host_backend/host_device.rs b/devices/src/usb/host_backend/host_device.rs index ae3faa0..ca00da8 100644 --- a/devices/src/usb/host_backend/host_device.rs +++ b/devices/src/usb/host_backend/host_device.rs @@ -83,7 +83,7 @@ impl HostToDeviceControlRequest { /// Host device is a device connected to host. pub struct HostDevice { - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, // Endpoints only contains data endpoints (1 to 30). Control transfers are handled at device // level. endpoints: Vec<UsbEndpoint>, @@ -106,7 +106,7 @@ impl Drop for HostDevice { impl HostDevice { /// Create a new host device. pub fn new( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: Arc<AsyncJobQueue>, device: LibUsbDevice, device_handle: DeviceHandle, diff --git a/devices/src/usb/host_backend/usb_endpoint.rs b/devices/src/usb/host_backend/usb_endpoint.rs index fcdacaa..ba1d359 100644 --- a/devices/src/usb/host_backend/usb_endpoint.rs +++ b/devices/src/usb/host_backend/usb_endpoint.rs @@ -22,7 +22,7 @@ use utils::FailHandle; /// Isochronous, Bulk or Interrupt endpoint. pub struct UsbEndpoint { - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: Arc<AsyncJobQueue>, device_handle: Arc<Mutex<DeviceHandle>>, endpoint_number: u8, @@ -33,7 +33,7 @@ pub struct UsbEndpoint { impl UsbEndpoint { /// Create new endpoint. This function will panic if endpoint type is control. pub fn new( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: Arc<AsyncJobQueue>, device_handle: Arc<Mutex<DeviceHandle>>, endpoint_number: u8, diff --git a/devices/src/usb/host_backend/utils.rs b/devices/src/usb/host_backend/utils.rs index 9f1abc9..60e29d7 100644 --- a/devices/src/usb/host_backend/utils.rs +++ b/devices/src/usb/host_backend/utils.rs @@ -44,7 +44,7 @@ pub fn update_transfer_state<T: UsbTransferBuffer>( /// Helper function to submit usb_transfer to device handle. pub fn submit_transfer<T: UsbTransferBuffer>( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, job_queue: &Arc<AsyncJobQueue>, xhci_transfer: Arc<XhciTransfer>, device_handle: &Arc<Mutex<DeviceHandle>>, diff --git a/devices/src/usb/xhci/device_slot.rs b/devices/src/usb/xhci/device_slot.rs index fcac44d..6f4e8f3 100644 --- a/devices/src/usb/xhci/device_slot.rs +++ b/devices/src/usb/xhci/device_slot.rs @@ -77,14 +77,14 @@ fn valid_endpoint_id(endpoint_id: u8) -> bool { #[derive(Clone)] pub struct DeviceSlots { - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, hub: Arc<UsbHub>, slots: Vec<Arc<DeviceSlot>>, } impl DeviceSlots { pub fn new( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, dcbaap: Register<u64>, hub: Arc<UsbHub>, interrupter: Arc<Mutex<Interrupter>>, @@ -336,7 +336,7 @@ impl DeviceSlot { /// Disable this device slot. If the slot is not enabled, callback will be invoked immediately /// with error. Otherwise, callback will be invoked when all trc is stopped. pub fn disable<C: FnMut(TrbCompletionCode) -> std::result::Result<(), ()> + 'static + Send>( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, slot: &Arc<DeviceSlot>, mut callback: C, ) -> Result<()> { @@ -577,7 +577,7 @@ impl DeviceSlot { pub fn reset_slot< C: FnMut(TrbCompletionCode) -> std::result::Result<(), ()> + 'static + Send, >( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, slot: &Arc<DeviceSlot>, mut callback: C, ) -> Result<()> { @@ -621,7 +621,7 @@ impl DeviceSlot { C: FnMut(TrbCompletionCode) -> std::result::Result<(), ()> + 'static + Send, >( &self, - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, endpoint_id: u8, mut cb: C, ) -> Result<()> { diff --git a/devices/src/usb/xhci/intr_resample_handler.rs b/devices/src/usb/xhci/intr_resample_handler.rs index 8c7ff99..d4faa71 100644 --- a/devices/src/usb/xhci/intr_resample_handler.rs +++ b/devices/src/usb/xhci/intr_resample_handler.rs @@ -25,7 +25,7 @@ impl IntrResampleHandler { interrupter, resample_evt, }); - let tmp_handler: Arc<EventHandler> = handler.clone(); + let tmp_handler: Arc<dyn EventHandler> = handler.clone(); if let Err(e) = event_loop.add_event( &handler.resample_evt, WatchingEvents::empty().set_read(), diff --git a/devices/src/usb/xhci/ring_buffer_controller.rs b/devices/src/usb/xhci/ring_buffer_controller.rs index 6452a57..36387fc 100644 --- a/devices/src/usb/xhci/ring_buffer_controller.rs +++ b/devices/src/usb/xhci/ring_buffer_controller.rs @@ -106,7 +106,7 @@ where event_loop: event_loop.clone(), event: evt, }); - let event_handler: Arc<EventHandler> = controller.clone(); + let event_handler: Arc<dyn EventHandler> = controller.clone(); event_loop .add_event( &controller.event, diff --git a/devices/src/usb/xhci/ring_buffer_stop_cb.rs b/devices/src/usb/xhci/ring_buffer_stop_cb.rs index 5a8eae7..8608601 100644 --- a/devices/src/usb/xhci/ring_buffer_stop_cb.rs +++ b/devices/src/usb/xhci/ring_buffer_stop_cb.rs @@ -26,7 +26,7 @@ impl RingBufferStopCallback { } struct RingBufferStopCallbackInner { - callback: Box<FnMut() + Send>, + callback: Box<dyn FnMut() + Send>, } impl Drop for RingBufferStopCallbackInner { @@ -38,7 +38,7 @@ impl Drop for RingBufferStopCallbackInner { /// Helper function to wrap up a closure with fail handle. The fail handle will be triggered if the /// closure returns an error. pub fn fallible_closure<E: std::fmt::Display, C: FnMut() -> Result<(), E> + 'static + Send>( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, mut callback: C, ) -> impl FnMut() + 'static + Send { move || match callback() { diff --git a/devices/src/usb/xhci/usb_hub.rs b/devices/src/usb/xhci/usb_hub.rs index dd0a6d9..37c59fb 100644 --- a/devices/src/usb/xhci/usb_hub.rs +++ b/devices/src/usb/xhci/usb_hub.rs @@ -74,7 +74,7 @@ pub struct UsbPort { portsc: Register<u32>, usbsts: Register<u32>, interrupter: Arc<Mutex<Interrupter>>, - backend_device: Mutex<Option<Box<XhciBackendDevice>>>, + backend_device: Mutex<Option<Box<dyn XhciBackendDevice>>>, } impl UsbPort { @@ -116,7 +116,7 @@ impl UsbPort { } /// Get current connected backend. - pub fn get_backend_device(&self) -> MutexGuard<Option<Box<XhciBackendDevice>>> { + pub fn get_backend_device(&self) -> MutexGuard<Option<Box<dyn XhciBackendDevice>>> { self.backend_device.lock() } @@ -131,7 +131,10 @@ impl UsbPort { Ok(()) } - fn attach(&self, device: Box<XhciBackendDevice>) -> std::result::Result<(), InterrupterError> { + fn attach( + &self, + device: Box<dyn XhciBackendDevice>, + ) -> std::result::Result<(), InterrupterError> { usb_debug!("A backend is connected to port {}", self.port_id); let mut locked = self.backend_device.lock(); assert!(locked.is_none()); @@ -250,7 +253,7 @@ impl UsbHub { } /// Connect backend to next empty port. - pub fn connect_backend(&self, backend: Box<XhciBackendDevice>) -> Result<u8> { + pub fn connect_backend(&self, backend: Box<dyn XhciBackendDevice>) -> Result<u8> { usb_debug!("Trying to connect backend to hub"); for port in &self.ports { if port.is_attached() { diff --git a/devices/src/usb/xhci/xhci.rs b/devices/src/usb/xhci/xhci.rs index b69397e..0ede18a 100644 --- a/devices/src/usb/xhci/xhci.rs +++ b/devices/src/usb/xhci/xhci.rs @@ -58,7 +58,7 @@ impl Display for Error { /// xHCI controller implementation. pub struct Xhci { - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, regs: XhciRegs, interrupter: Arc<Mutex<Interrupter>>, command_ring_controller: Arc<CommandRingController>, @@ -75,7 +75,7 @@ pub struct Xhci { impl Xhci { /// Create a new xHCI controller. pub fn new( - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, mem: GuestMemory, device_provider: HostBackendDeviceProvider, irq_evt: EventFd, diff --git a/devices/src/usb/xhci/xhci_backend_device_provider.rs b/devices/src/usb/xhci/xhci_backend_device_provider.rs index 2c9d4bf..9f186ec 100644 --- a/devices/src/usb/xhci/xhci_backend_device_provider.rs +++ b/devices/src/usb/xhci/xhci_backend_device_provider.rs @@ -12,7 +12,7 @@ pub trait XhciBackendDeviceProvider: Send { /// Start the provider on EventLoop. fn start( &mut self, - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, event_loop: Arc<EventLoop>, hub: Arc<UsbHub>, ) -> std::result::Result<(), ()>; diff --git a/devices/src/usb/xhci/xhci_controller.rs b/devices/src/usb/xhci/xhci_controller.rs index f920704..2c8ba58 100644 --- a/devices/src/usb/xhci/xhci_controller.rs +++ b/devices/src/usb/xhci/xhci_controller.rs @@ -87,7 +87,7 @@ enum XhciControllerState { // Xhci init could fail. #[allow(dead_code)] xhci: Option<Arc<Xhci>>, - fail_handle: Arc<FailHandle>, + fail_handle: Arc<dyn FailHandle>, }, } @@ -131,7 +131,7 @@ impl XhciController { irq_resample_evt, } => { let (mmio, regs) = init_xhci_mmio_space_and_regs(); - let fail_handle: Arc<FailHandle> = Arc::new(XhciFailHandle::new(®s)); + let fail_handle: Arc<dyn FailHandle> = Arc::new(XhciFailHandle::new(®s)); let xhci = match Xhci::new( fail_handle.clone(), self.mem.clone(), diff --git a/devices/src/usb/xhci/xhci_transfer.rs b/devices/src/usb/xhci/xhci_transfer.rs index 76fecdc..76454af 100644 --- a/devices/src/usb/xhci/xhci_transfer.rs +++ b/devices/src/usb/xhci/xhci_transfer.rs @@ -66,7 +66,7 @@ pub enum XhciTransferState { /// When transfer is submitted, it will contain a transfer callback, which should be invoked /// when the transfer is cancelled. Submitted { - cancel_callback: Box<FnMut() + Send>, + cancel_callback: Box<dyn FnMut() + Send>, }, Cancelling, Cancelled, diff --git a/devices/src/utils/async_job_queue.rs b/devices/src/utils/async_job_queue.rs index 8f9f4a3..103d705 100644 --- a/devices/src/utils/async_job_queue.rs +++ b/devices/src/utils/async_job_queue.rs @@ -11,7 +11,7 @@ use sys_util::{EventFd, WatchingEvents}; /// Async Job Queue can schedule async jobs. pub struct AsyncJobQueue { - jobs: Mutex<Vec<Box<FnMut() + 'static + Send>>>, + jobs: Mutex<Vec<Box<dyn FnMut() + Send>>>, evt: EventFd, } @@ -23,7 +23,7 @@ impl AsyncJobQueue { jobs: Mutex::new(Vec::new()), evt, }); - let handler: Arc<EventHandler> = queue.clone(); + let handler: Arc<dyn EventHandler> = queue.clone(); event_loop.add_event( &queue.evt, WatchingEvents::empty().set_read(), diff --git a/devices/src/utils/event_loop.rs b/devices/src/utils/event_loop.rs index 99fe1c9..34aec61 100644 --- a/devices/src/utils/event_loop.rs +++ b/devices/src/utils/event_loop.rs @@ -19,7 +19,7 @@ pub trait FailHandle: Send + Sync { fn failed(&self) -> bool; } -impl FailHandle for Option<Arc<FailHandle>> { +impl FailHandle for Option<Arc<dyn FailHandle>> { fn fail(&self) { match *self { Some(ref handle) => handle.fail(), @@ -57,9 +57,9 @@ impl PollToken for Fd { /// EpollEventLoop is an event loop blocked on a set of fds. When a monitered events is triggered, /// event loop will invoke the mapped handler. pub struct EventLoop { - fail_handle: Option<Arc<FailHandle>>, + fail_handle: Option<Arc<dyn FailHandle>>, poll_ctx: Arc<EpollContext<Fd>>, - handlers: Arc<Mutex<BTreeMap<RawFd, Weak<EventHandler>>>>, + handlers: Arc<Mutex<BTreeMap<RawFd, Weak<dyn EventHandler>>>>, stop_evt: EventFd, } @@ -71,13 +71,13 @@ pub trait EventHandler: Send + Sync { impl EventLoop { /// Start an event loop. An optional fail handle could be passed to the event loop. pub fn start( - fail_handle: Option<Arc<FailHandle>>, + fail_handle: Option<Arc<dyn FailHandle>>, ) -> Result<(EventLoop, thread::JoinHandle<()>)> { let (self_stop_evt, stop_evt) = EventFd::new() .and_then(|e| Ok((e.try_clone()?, e))) .map_err(Error::CreateEventFd)?; - let fd_callbacks: Arc<Mutex<BTreeMap<RawFd, Weak<EventHandler>>>> = + let fd_callbacks: Arc<Mutex<BTreeMap<RawFd, Weak<dyn EventHandler>>>> = Arc::new(Mutex::new(BTreeMap::new())); let poll_ctx: EpollContext<Fd> = EpollContext::new() .and_then(|pc| pc.add(&stop_evt, Fd(stop_evt.as_raw_fd())).and(Ok(pc))) @@ -156,9 +156,9 @@ impl EventLoop { /// the event will be removed. pub fn add_event( &self, - fd: &AsRawFd, + fd: &dyn AsRawFd, events: WatchingEvents, - handler: Weak<EventHandler>, + handler: Weak<dyn EventHandler>, ) -> Result<()> { if self.fail_handle.failed() { return Err(Error::EventLoopAlreadyFailed); @@ -173,7 +173,7 @@ impl EventLoop { /// Removes event for this `fd`. This function returns false if it fails. /// /// EventLoop does not guarantee all events for `fd` is handled. - pub fn remove_event_for_fd(&self, fd: &AsRawFd) -> Result<()> { + pub fn remove_event_for_fd(&self, fd: &dyn AsRawFd) -> Result<()> { if self.fail_handle.failed() { return Err(Error::EventLoopAlreadyFailed); } diff --git a/devices/src/virtio/gpu/backend.rs b/devices/src/virtio/gpu/backend.rs index ea7faff..8d4d406 100644 --- a/devices/src/virtio/gpu/backend.rs +++ b/devices/src/virtio/gpu/backend.rs @@ -312,7 +312,7 @@ pub struct Backend { display: Rc<RefCell<GpuDisplay>>, device: Device, renderer: Renderer, - resources: Map<u32, Box<VirglResource>>, + resources: Map<u32, Box<dyn VirglResource>>, contexts: Map<u32, RendererContext>, scanout_surface: Option<u32>, cursor_surface: Option<u32>, diff --git a/devices/src/virtio/virtio_device.rs b/devices/src/virtio/virtio_device.rs index f497146..d5d5266 100644 --- a/devices/src/virtio/virtio_device.rs +++ b/devices/src/virtio/virtio_device.rs @@ -82,7 +82,7 @@ pub trait VirtioDevice: Send { } /// Returns any additional capabiltiies required by the device. - fn get_device_caps(&self) -> Vec<Box<PciCapability>> { + fn get_device_caps(&self) -> Vec<Box<dyn PciCapability>> { Vec::new() } } diff --git a/devices/src/virtio/virtio_pci_common_config.rs b/devices/src/virtio/virtio_pci_common_config.rs index fb428dd..59b2b0a 100644 --- a/devices/src/virtio/virtio_pci_common_config.rs +++ b/devices/src/virtio/virtio_pci_common_config.rs @@ -43,7 +43,7 @@ impl VirtioPciCommonConfig { offset: u64, data: &mut [u8], queues: &mut Vec<Queue>, - device: &mut Box<VirtioDevice>, + device: &mut dyn VirtioDevice, ) { match data.len() { 1 => { @@ -71,7 +71,7 @@ impl VirtioPciCommonConfig { offset: u64, data: &[u8], queues: &mut Vec<Queue>, - device: &mut Box<VirtioDevice>, + device: &mut dyn VirtioDevice, ) { match data.len() { 1 => self.write_common_config_byte(offset, data[0]), @@ -133,7 +133,7 @@ impl VirtioPciCommonConfig { } } - fn read_common_config_dword(&self, offset: u64, device: &Box<VirtioDevice>) -> u32 { + fn read_common_config_dword(&self, offset: u64, device: &dyn VirtioDevice) -> u32 { match offset { 0x00 => self.device_feature_select, 0x04 => { @@ -155,7 +155,7 @@ impl VirtioPciCommonConfig { offset: u64, value: u32, queues: &mut Vec<Queue>, - device: &mut Box<VirtioDevice>, + device: &mut dyn VirtioDevice, ) { fn hi(v: &mut GuestAddress, x: u32) { *v = (*v & 0xffffffff) | ((x as u64) << 32) @@ -267,41 +267,41 @@ mod tests { queue_select: 0xff, }; - let mut dev: Box<VirtioDevice> = Box::new(DummyDevice(0)); + let dev = &mut DummyDevice(0) as &mut dyn VirtioDevice; let mut queues = Vec::new(); // Can set all bits of driver_status. - regs.write(0x14, &[0x55], &mut queues, &mut dev); + regs.write(0x14, &[0x55], &mut queues, dev); let mut read_back = vec![0x00]; - regs.read(0x14, &mut read_back, &mut queues, &mut dev); + regs.read(0x14, &mut read_back, &mut queues, dev); assert_eq!(read_back[0], 0x55); // The config generation register is read only. - regs.write(0x15, &[0xaa], &mut queues, &mut dev); + regs.write(0x15, &[0xaa], &mut queues, dev); let mut read_back = vec![0x00]; - regs.read(0x15, &mut read_back, &mut queues, &mut dev); + regs.read(0x15, &mut read_back, &mut queues, dev); assert_eq!(read_back[0], 0x55); // Device features is read-only and passed through from the device. - regs.write(0x04, &[0, 0, 0, 0], &mut queues, &mut dev); + regs.write(0x04, &[0, 0, 0, 0], &mut queues, dev); let mut read_back = vec![0, 0, 0, 0]; - regs.read(0x04, &mut read_back, &mut queues, &mut dev); + regs.read(0x04, &mut read_back, &mut queues, dev); assert_eq!(LittleEndian::read_u32(&read_back), DUMMY_FEATURES as u32); // Feature select registers are read/write. - regs.write(0x00, &[1, 2, 3, 4], &mut queues, &mut dev); + regs.write(0x00, &[1, 2, 3, 4], &mut queues, dev); let mut read_back = vec![0, 0, 0, 0]; - regs.read(0x00, &mut read_back, &mut queues, &mut dev); + regs.read(0x00, &mut read_back, &mut queues, dev); assert_eq!(LittleEndian::read_u32(&read_back), 0x0403_0201); - regs.write(0x08, &[1, 2, 3, 4], &mut queues, &mut dev); + regs.write(0x08, &[1, 2, 3, 4], &mut queues, dev); let mut read_back = vec![0, 0, 0, 0]; - regs.read(0x08, &mut read_back, &mut queues, &mut dev); + regs.read(0x08, &mut read_back, &mut queues, dev); assert_eq!(LittleEndian::read_u32(&read_back), 0x0403_0201); // 'queue_select' can be read and written. - regs.write(0x16, &[0xaa, 0x55], &mut queues, &mut dev); + regs.write(0x16, &[0xaa, 0x55], &mut queues, dev); let mut read_back = vec![0x00, 0x00]; - regs.read(0x16, &mut read_back, &mut queues, &mut dev); + regs.read(0x16, &mut read_back, &mut queues, dev); assert_eq!(read_back[0], 0xaa); assert_eq!(read_back[1], 0x55); } diff --git a/devices/src/virtio/virtio_pci_device.rs b/devices/src/virtio/virtio_pci_device.rs index 4ba5437..27ab5fb 100644 --- a/devices/src/virtio/virtio_pci_device.rs +++ b/devices/src/virtio/virtio_pci_device.rs @@ -149,7 +149,7 @@ const VIRTIO_PCI_DEVICE_ID_BASE: u16 = 0x1040; // Add to device type to get devi pub struct VirtioPciDevice { config_regs: PciConfiguration, - device: Box<VirtioDevice>, + device: Box<dyn VirtioDevice>, device_activated: bool, interrupt_status: Arc<AtomicUsize>, @@ -165,7 +165,7 @@ pub struct VirtioPciDevice { impl VirtioPciDevice { /// Constructs a new PCI transport for the given virtio device. - pub fn new(mem: GuestMemory, device: Box<VirtioDevice>) -> Result<Self> { + pub fn new(mem: GuestMemory, device: Box<dyn VirtioDevice>) -> Result<Self> { let mut queue_evts = Vec::new(); for _ in device.queue_max_sizes().iter() { queue_evts.push(EventFd::new()?) @@ -414,7 +414,7 @@ impl PciDevice for VirtioPciDevice { o - COMMON_CONFIG_BAR_OFFSET, data, &mut self.queues, - &mut self.device, + self.device.as_mut(), ) } o if ISR_CONFIG_BAR_OFFSET <= o && o < ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE => { @@ -448,7 +448,7 @@ impl PciDevice for VirtioPciDevice { o - COMMON_CONFIG_BAR_OFFSET, data, &mut self.queues, - &mut self.device, + self.device.as_mut(), ) } o if ISR_CONFIG_BAR_OFFSET <= o && o < ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE => { diff --git a/devices/src/virtio/wl.rs b/devices/src/virtio/wl.rs index a9498fe..870b5ea 100644 --- a/devices/src/virtio/wl.rs +++ b/devices/src/virtio/wl.rs @@ -32,6 +32,7 @@ use std::cell::RefCell; use std::collections::btree_map::Entry; use std::collections::{BTreeMap as Map, BTreeSet as Set, VecDeque}; use std::convert::From; +use std::error::Error as StdError; use std::ffi::CStr; use std::fmt::{self, Display}; use std::fs::File; @@ -645,7 +646,7 @@ enum WlResp<'a> { VfdHup { id: u32, }, - Err(Box<std::error::Error>), + Err(Box<dyn StdError>), OutOfMemory, InvalidId, InvalidType, @@ -891,11 +892,11 @@ impl WlVfd { } // The FD that is used for polling for events on this VFD. - fn poll_fd(&self) -> Option<&AsRawFd> { - self.socket + fn poll_fd(&self) -> Option<&dyn AsRawFd> { + self.socket.as_ref().map(|s| s as &dyn AsRawFd).or(self + .local_pipe .as_ref() - .map(|s| s as &AsRawFd) - .or(self.local_pipe.as_ref().map(|&(_, ref p)| p as &AsRawFd)) + .map(|&(_, ref p)| p as &dyn AsRawFd)) } // Sends data/files from the guest to the host over this VFD. |