rust: add preludes to all crates
Subsystems (2)
cover rust: add preludes to all crates
This series adds a prelude module to all crates, so that it becomes possible to import from each crate with either of the following
use XYZ::prelude::*;
use XYZ::{self, prelude::*};
The latter is used for items that have a "too common" name to be put in the prelude: util::Error, util::Result, migration::Infallible.
Thanks,
Paolo
Paolo Bonzini (8):
util: add prelude
common: add prelude
hwcore: add prelude
migration: add prelude
chardev: add prelude
bql: add prelude
system: add prelude
qom: add more to the prelude
rust/bql/meson.build | 1 + rust/bql/src/lib.rs | 5 +++++ rust/bql/src/prelude.rs | 4 ++++ rust/chardev/meson.build | 1 + rust/chardev/src/chardev.rs | 2 +- rust/chardev/src/lib.rs | 5 +++++ rust/chardev/src/prelude.rs | 5 +++++ rust/common/meson.build | 13 +------------ rust/common/src/lib.rs | 5 +++++ rust/common/src/prelude.rs | 9 +++++++++ rust/hw/char/pl011/src/device.rs | 22 ++++++++-------------- rust/hw/core/meson.build | 1 + rust/hw/core/src/lib.rs | 5 +++++ rust/hw/core/src/prelude.rs | 14 ++++++++++++++ rust/hw/core/src/qdev.rs | 2 +- rust/hw/core/src/sysbus.rs | 2 +- rust/hw/core/tests/tests.rs | 8 ++++---- rust/hw/timer/hpet/src/device.rs | 26 +++++++++----------------- rust/hw/timer/hpet/src/fw_cfg.rs | 2 +- rust/migration/meson.build | 1 + rust/migration/src/lib.rs | 5 +++++ rust/migration/src/migratable.rs | 2 +- rust/migration/src/prelude.rs | 19 +++++++++++++++++++ rust/qom/src/prelude.rs | 4 ++++ rust/system/meson.build | 1 + rust/system/src/lib.rs | 5 +++++ rust/system/src/prelude.rs | 8 ++++++++ rust/tests/tests/vmstate_tests.rs | 7 +++---- rust/util/meson.build | 1 + rust/util/src/lib.rs | 5 +++++ rust/util/src/prelude.rs | 11 +++++++++++ 31 files changed, 145 insertions(+), 56 deletions(-) create mode 100644 rust/bql/src/prelude.rs create mode 100644 rust/chardev/src/prelude.rs create mode 100644 rust/common/src/prelude.rs create mode 100644 rust/hw/core/src/prelude.rs create mode 100644 rust/migration/src/prelude.rs create mode 100644 rust/system/src/prelude.rs create mode 100644 rust/util/src/prelude.rs
Signature · 1 line
2.51.1
PATCH 1/8 util: add prelude
--- rust/hw/char/pl011/src/device.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 5 +---- rust/hw/timer/hpet/src/fw_cfg.rs | 2 +- rust/util/meson.build | 1 + rust/util/src/lib.rs | 5 +++++ rust/util/src/prelude.rs | 11 +++++++++++ 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 rust/util/src/prelude.rs
rust/hw/char/pl011/src/device.rs +1 -1
@@ -17,7 +17,7 @@ };use qom::{prelude::*,ObjectImpl ,Owned ,ParentField ,ParentInit };use system::{hwaddr,MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder }; -use util::{log::Log , log_mask_ln,ResultExt }; +use util::prelude::*;use crate ::registers::{self,Interrupt ,RegisterOffset };
rust/hw/timer/hpet/src/device.rs +1 -4
@@ -25,10 +25,7 @@ bindings::{address_space_memory, address_space_stl_le, hwaddr}, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder, MEMTXATTRS_UNSPECIFIED, }; -use util::{ - ensure, - timer::{Timer ,CLOCK_VIRTUAL ,NANOSECONDS_PER_SECOND }, -}; +use util::prelude::*;use crate ::fw_cfg::HPETFwConfig ;
rust/hw/timer/hpet/src/fw_cfg.rs +1 -1
@@ -5,7 +5,7 @@use std::ptr::addr_of_mut;use common::Zeroable ; -use util::{self, ensure}; +use util::{self, prelude::*};/// Each `HPETState` represents a Event Timer Block. The v1 spec supports /// up to 8 blocks. QEMU only uses 1 block (in PC machine).
rust/util/meson.build +1 -0
@@ -34,6 +34,7 @@ _util_rs = static_library( 'src/error.rs', 'src/log.rs', 'src/module.rs', + 'src/prelude.rs', 'src/timer.rs', ], {'.': _util_bindings_inc_rs}
rust/util/src/lib.rs +5 -0
@@ -4,6 +4,11 @@pub mod error;pub mod log;pub mod module; + +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude;pub mod timer;pub use error::{Error ,Result ,ResultExt };
rust/util/src/prelude.rs new file +11 -0
@@ -0,0 +1,11 @@ +//! Essential types and traits intended for blanket imports. + +pub use crate ::error::ResultExt ; +pub use crate ::log::Log ; +pub use crate ::timer::Timer ; +pub use crate ::timer::CLOCK_VIRTUAL ; +pub use crate ::timer::NANOSECONDS_PER_SECOND ; + +// Re-export commonly used macros +pub use crate ::ensure; +pub use crate ::log_mask_ln;
Signature · 1 line
2.51.1
PATCH 2/8 common: add prelude
In reply to Paolo Bonzini--- rust/common/meson.build | 13 +------------ rust/common/src/lib.rs | 5 +++++ rust/common/src/prelude.rs | 9 +++++++++ rust/hw/char/pl011/src/device.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 rust/common/src/prelude.rs
rust/common/meson.build +1 -12
@@ -4,18 +4,7 @@ _common_cfg = run_command(rustc_args, _common_rs = static_library( 'common', - structured_sources( - [ - 'src/lib.rs', - 'src/assertions.rs', - 'src/bitops.rs', - 'src/callbacks.rs', - 'src/errno.rs', - 'src/opaque.rs', - 'src/uninit.rs', - 'src/zeroable.rs', - ], - ), + 'src/lib.rs', override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', rust_args: _common_cfg,
rust/common/src/lib.rs +5 -0
@@ -15,6 +15,11 @@pub mod opaque;pub use opaque::{Opaque ,Wrapper }; +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude; +pub mod uninit;pub use uninit::MaybeUninitField ;
rust/common/src/prelude.rs new file +9 -0
@@ -0,0 +1,9 @@ +//! Essential types and traits intended for blanket imports. + +pub use crate ::bitops::IntegerExt ; +pub use crate ::uninit::MaybeUninitField ; + +// Re-export commonly used macros +pub use crate ::static_assert; +pub use crate ::uninit_field_mut; +pub use qemu_macros::TryInto ;
rust/hw/char/pl011/src/device.rs +1 -1
@@ -6,7 +6,7 @@use bql::BqlRefCell ;use chardev::{CharFrontend ,Chardev ,Event }; -use common::{static_assert, uninit_field_mut}; +use common::prelude::*;use hwcore::{Clock ,ClockEvent ,DeviceImpl ,DeviceMethods ,DeviceState ,IRQState ,InterruptSource ,ResetType ,ResettablePhasesImpl ,SysBusDevice ,SysBusDeviceImpl ,SysBusDeviceMethods ,
rust/hw/timer/hpet/src/device.rs +1 -1
@@ -11,7 +11,7 @@ };use bql::{BqlCell ,BqlRefCell }; -use common::{bitops::IntegerExt , uninit_field_mut}; +use common::prelude::*;use hwcore::{DeviceImpl ,DeviceMethods ,DeviceState ,InterruptSource ,ResetType ,ResettablePhasesImpl ,SysBusDevice ,SysBusDeviceImpl ,SysBusDeviceMethods ,
Signature · 1 line
2.51.1
PATCH 3/8 hwcore: add prelude
In reply to Paolo Bonzini--- rust/hw/char/pl011/src/device.rs | 5 +---- rust/hw/core/meson.build | 1 + rust/hw/core/src/lib.rs | 5 +++++ rust/hw/core/src/prelude.rs | 14 ++++++++++++++ rust/hw/core/tests/tests.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 5 +---- 6 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 rust/hw/core/src/prelude.rs
rust/hw/char/pl011/src/device.rs +1 -4
@@ -7,10 +7,7 @@use bql::BqlRefCell ;use chardev::{CharFrontend ,Chardev ,Event };use common::prelude::*; -use hwcore::{ -Clock ,ClockEvent ,DeviceImpl ,DeviceMethods ,DeviceState ,IRQState ,InterruptSource , -ResetType ,ResettablePhasesImpl ,SysBusDevice ,SysBusDeviceImpl ,SysBusDeviceMethods , -}; +use hwcore::{prelude::*,ClockEvent ,IRQState };use migration::{ self, impl_vmstate_forward, impl_vmstate_struct, vmstate_fields, vmstate_of, vmstate_subsections, vmstate_unused,VMStateDescription ,VMStateDescriptionBuilder ,
rust/hw/core/meson.build +1 -0
@@ -51,6 +51,7 @@ _hwcore_rs = static_library( 'src/lib.rs', 'src/bindings.rs', 'src/irq.rs', + 'src/prelude.rs', 'src/qdev.rs', 'src/sysbus.rs', ],
rust/hw/core/src/lib.rs +5 -0
@@ -8,6 +8,11 @@mod irq;pub use irq::*; +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude; +mod qdev;pub use qdev::*;
rust/hw/core/src/prelude.rs new file +14 -0
@@ -0,0 +1,14 @@ +//! Essential types and traits intended for blanket imports. + +pub use crate ::qdev::Clock ; +pub use crate ::qdev::DeviceState ; +pub use crate ::qdev::DeviceImpl ; +pub use crate ::qdev::DeviceMethods ; +pub use crate ::qdev::ResettablePhasesImpl ; +pub use crate ::qdev::ResetType ; + +pub use crate ::sysbus::SysBusDevice ; +pub use crate ::sysbus::SysBusDeviceImpl ; +pub use crate ::sysbus::SysBusDeviceMethods ; + +pub use crate ::irq::InterruptSource ;
rust/hw/core/tests/tests.rs +1 -1
@@ -5,7 +5,7 @@use std::{ffi::CStr , ptr::addr_of};use bql::BqlCell ; -use hwcore::{DeviceImpl ,DeviceState ,ResettablePhasesImpl ,SysBusDevice }; +use hwcore::prelude::*;use migration::{VMStateDescription ,VMStateDescriptionBuilder };use qom::{prelude::*,ObjectImpl ,ParentField };use util::bindings::{module_call_init, module_init_type};
rust/hw/timer/hpet/src/device.rs +1 -4
@@ -12,10 +12,7 @@use bql::{BqlCell ,BqlRefCell };use common::prelude::*; -use hwcore::{ -DeviceImpl ,DeviceMethods ,DeviceState ,InterruptSource ,ResetType ,ResettablePhasesImpl , -SysBusDevice ,SysBusDeviceImpl ,SysBusDeviceMethods , -}; +use hwcore::prelude::*;use migration::{ self, impl_vmstate_struct, vmstate_fields, vmstate_of, vmstate_subsections, vmstate_validate,VMStateDescription ,VMStateDescriptionBuilder ,
Signature · 1 line
2.51.1
PATCH 4/8 migration: add prelude
In reply to Paolo Bonzini--- rust/chardev/src/prelude.rs | 3 +++ rust/hw/char/pl011/src/device.rs | 5 +---- rust/hw/core/tests/tests.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 5 +---- rust/migration/meson.build | 1 + rust/migration/src/lib.rs | 5 +++++ rust/migration/src/prelude.rs | 19 +++++++++++++++++++ rust/tests/tests/vmstate_tests.rs | 5 ++--- 8 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 rust/chardev/src/prelude.rs create mode 100644 rust/migration/src/prelude.rs
rust/chardev/src/prelude.rs new file +3 -0
@@ -0,0 +1,3 @@ +//! Essential types and traits intended for blanket imports. + +pub use crate ::chardev::{Chardev ,CharFrontend ,Event };
rust/hw/char/pl011/src/device.rs +1 -4
@@ -8,10 +8,7 @@use chardev::{CharFrontend ,Chardev ,Event };use common::prelude::*;use hwcore::{prelude::*,ClockEvent ,IRQState }; -use migration::{ - self, impl_vmstate_forward, impl_vmstate_struct, vmstate_fields, vmstate_of, - vmstate_subsections, vmstate_unused,VMStateDescription ,VMStateDescriptionBuilder , -}; +use migration::{self, prelude::*};use qom::{prelude::*,ObjectImpl ,Owned ,ParentField ,ParentInit };use system::{hwaddr,MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder };use util::prelude::*;
rust/hw/core/tests/tests.rs +1 -1
@@ -6,7 +6,7 @@use bql::BqlCell ;use hwcore::prelude::*; -use migration::{VMStateDescription ,VMStateDescriptionBuilder }; +use migration::prelude::*;use qom::{prelude::*,ObjectImpl ,ParentField };use util::bindings::{module_call_init, module_init_type};
rust/hw/timer/hpet/src/device.rs +1 -4
@@ -13,10 +13,7 @@use bql::{BqlCell ,BqlRefCell };use common::prelude::*;use hwcore::prelude::*; -use migration::{ - self, impl_vmstate_struct, vmstate_fields, vmstate_of, vmstate_subsections, vmstate_validate, -VMStateDescription ,VMStateDescriptionBuilder , -}; +use migration::{self, prelude::*};use qom::{prelude::*,ObjectImpl ,ParentField ,ParentInit };use system::{ bindings::{address_space_memory, address_space_stl_le, hwaddr},
rust/migration/meson.build +1 -0
@@ -32,6 +32,7 @@ _migration_rs = static_library( 'src/lib.rs', 'src/bindings.rs', 'src/migratable.rs', + 'src/prelude.rs', 'src/vmstate.rs', ], {'.' : _migration_bindings_inc_rs},
rust/migration/src/lib.rs +5 -0
@@ -7,5 +7,10 @@pub mod migratable;pub use migratable::*; +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude; +pub mod vmstate;pub use vmstate::*;
rust/migration/src/prelude.rs new file +19 -0
@@ -0,0 +1,19 @@ +//! Essential types and traits intended for blanket imports. + +// Core migration traits and types +pub use crate ::vmstate::VMState ; +pub use crate ::vmstate::VMStateDescription ; +pub use crate ::vmstate::VMStateDescriptionBuilder ; + +// Migratable wrappers +pub use crate ::migratable::Migratable ; +pub use crate ::ToMigrationState ; + +// Commonly used macros +pub use crate ::impl_vmstate_forward; +pub use crate ::impl_vmstate_struct; +pub use crate ::vmstate_fields; +pub use crate ::vmstate_of; +pub use crate ::vmstate_subsections; +pub use crate ::vmstate_unused; +pub use crate ::vmstate_validate;
rust/tests/tests/vmstate_tests.rs +2 -3
@@ -16,9 +16,8 @@ vmstate_info_bool, vmstate_info_int32, vmstate_info_int64, vmstate_info_int8, vmstate_info_uint64, vmstate_info_uint8, vmstate_info_unused_buffer, VMStateFlags, }, - impl_vmstate_forward, impl_vmstate_struct, - vmstate::{VMStateDescription, VMStateDescriptionBuilder, VMStateField}, - vmstate_fields, vmstate_of, vmstate_unused, vmstate_validate, + prelude::*, + vmstate::VMStateField, };const FOO_ARRAY_MAX :usize =3 ;
Signature · 1 line
2.51.1
PATCH 5/8 chardev: add prelude
In reply to Paolo Bonzini--- rust/chardev/meson.build | 1 + rust/chardev/src/lib.rs | 5 +++++ rust/chardev/src/prelude.rs | 4 +++- rust/hw/char/pl011/src/device.rs | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-)
rust/chardev/meson.build +1 -0
@@ -30,6 +30,7 @@ _chardev_rs = static_library( 'src/lib.rs', 'src/bindings.rs', 'src/chardev.rs', + 'src/prelude.rs', ], {'.': _chardev_bindings_inc_rs} ),
rust/chardev/src/lib.rs +5 -0
@@ -4,3 +4,8 @@mod chardev;pub use chardev::*; + +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude;
rust/chardev/src/prelude.rs +3 -1
@@ -1,3 +1,5 @@//! Essential types and traits intended for blanket imports. -pub use crate ::chardev::{Chardev ,CharFrontend ,Event }; +pub use crate ::chardev::Chardev ; +pub use crate ::chardev::CharFrontend ; +pub use crate ::chardev::Event ;
rust/hw/char/pl011/src/device.rs +1 -1
@@ -5,7 +5,7 @@use std::{ffi::CStr , mem::size_of};use bql::BqlRefCell ; -use chardev::{CharFrontend ,Chardev ,Event }; +use chardev::prelude::*;use common::prelude::*;use hwcore::{prelude::*,ClockEvent ,IRQState };use migration::{self, prelude::*};
Signature · 1 line
2.51.1
PATCH 6/8 bql: add prelude
In reply to Paolo Bonzini--- rust/bql/meson.build | 1 + rust/bql/src/lib.rs | 5 +++++ rust/bql/src/prelude.rs | 4 ++++ rust/chardev/src/chardev.rs | 2 +- rust/hw/char/pl011/src/device.rs | 2 +- rust/hw/core/tests/tests.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 2 +- rust/migration/src/migratable.rs | 2 +- rust/tests/tests/vmstate_tests.rs | 2 +- 9 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 rust/bql/src/prelude.rs
rust/bql/meson.build +1 -0
@@ -31,6 +31,7 @@ _bql_rs = static_library( 'src/lib.rs', 'src/bindings.rs', 'src/cell.rs', + 'src/prelude.rs', ], {'.': _bql_bindings_inc_rs} ),
rust/bql/src/lib.rs +5 -0
@@ -6,6 +6,11 @@mod cell;pub use cell::*; +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude; +/// An internal function that is used by doctests. pub fn start_test () {// SAFETY: integration tests are run with --test-threads=1, while
rust/bql/src/prelude.rs new file +4 -0
@@ -0,0 +1,4 @@ +//! Essential types and traits intended for blanket imports. + +pub use crate ::cell::BqlCell ; +pub use crate ::cell::BqlRefCell ;
rust/chardev/src/chardev.rs +1 -1
@@ -18,7 +18,7 @@ slice, }; -use bql::{BqlRefCell ,BqlRefMut }; +use bql::{prelude::*,BqlRefMut };use common::{callbacks::FnCall , errno,Opaque };use qom::prelude::*;
rust/hw/char/pl011/src/device.rs +1 -1
@@ -4,7 +4,7 @@use std::{ffi::CStr , mem::size_of}; -use bql::BqlRefCell ; +use bql::prelude::*;use chardev::prelude::*;use common::prelude::*;use hwcore::{prelude::*,ClockEvent ,IRQState };
rust/hw/core/tests/tests.rs +1 -1
@@ -4,7 +4,7 @@use std::{ffi::CStr , ptr::addr_of}; -use bql::BqlCell ; +use bql::prelude::*;use hwcore::prelude::*;use migration::prelude::*;use qom::{prelude::*,ObjectImpl ,ParentField };
rust/hw/timer/hpet/src/device.rs +1 -1
@@ -10,7 +10,7 @@ slice::from_ref, }; -use bql::{BqlCell ,BqlRefCell }; +use bql::prelude::*;use common::prelude::*;use hwcore::prelude::*;use migration::{self, prelude::*};
rust/migration/src/migratable.rs +1 -1
@@ -9,7 +9,7 @@ sync::{Arc, Mutex}, }; -use bql::{BqlCell ,BqlRefCell }; +use bql::prelude::*;use common::Zeroable ;use crate ::{
rust/tests/tests/vmstate_tests.rs +1 -1
@@ -9,7 +9,7 @@ slice, }; -use bql::BqlCell ; +use bql::prelude::*;use common::Opaque ;use migration::{ bindings::{
Signature · 1 line
2.51.1
PATCH 7/8 system: add prelude
In reply to Paolo Bonzini--- rust/hw/char/pl011/src/device.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 5 +++-- rust/system/meson.build | 1 + rust/system/src/lib.rs | 5 +++++ rust/system/src/prelude.rs | 8 ++++++++ 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 rust/system/src/prelude.rs
rust/hw/char/pl011/src/device.rs +1 -1
@@ -10,7 +10,7 @@use hwcore::{prelude::*,ClockEvent ,IRQState };use migration::{self, prelude::*};use qom::{prelude::*,ObjectImpl ,Owned ,ParentField ,ParentInit }; -use system::{hwaddr,MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder }; +use system::prelude::*;use util::prelude::*;use crate ::registers::{self,Interrupt ,RegisterOffset };
rust/hw/timer/hpet/src/device.rs +3 -2
@@ -16,8 +16,9 @@use migration::{self, prelude::*};use qom::{prelude::*,ObjectImpl ,ParentField ,ParentInit };use system::{ - bindings::{address_space_memory, address_space_stl_le, hwaddr}, -MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder ,MEMTXATTRS_UNSPECIFIED , + bindings::{address_space_memory, address_space_stl_le}, +MEMTXATTRS_UNSPECIFIED , + prelude::*, };use util::prelude::*;
rust/system/meson.build +1 -0
@@ -30,6 +30,7 @@ _system_rs = static_library( 'src/lib.rs', 'src/bindings.rs', 'src/memory.rs', + 'src/prelude.rs', ], {'.': _system_bindings_inc_rs} ),
rust/system/src/lib.rs +5 -0
@@ -4,3 +4,8 @@mod memory;pub use memory::*; + +// preserve one-item-per-"use" syntax, it is clearer +// for prelude-like modules +#[rustfmt::skip] +pub mod prelude;
rust/system/src/prelude.rs new file +8 -0
@@ -0,0 +1,8 @@ +//! Essential types and traits intended for blanket imports. + +// Core memory types +pub use crate ::memory::hwaddr; +pub use crate ::memory::MemoryRegion ; +pub use crate ::memory::MemoryRegionOps ; +pub use crate ::memory::MemoryRegionOpsBuilder ; +pub use crate ::memory::MemTxAttrs ;
Signature · 1 line
2.51.1
PATCH 8/8 qom: add more to the prelude
In reply to Paolo Bonzini--- rust/hw/char/pl011/src/device.rs | 2 +- rust/hw/core/src/qdev.rs | 2 +- rust/hw/core/src/sysbus.rs | 2 +- rust/hw/core/tests/tests.rs | 2 +- rust/hw/timer/hpet/src/device.rs | 2 +- rust/qom/src/prelude.rs | 4 ++++ 6 files changed, 9 insertions(+), 5 deletions(-)
rust/hw/char/pl011/src/device.rs +1 -1
@@ -9,7 +9,7 @@use common::prelude::*;use hwcore::{prelude::*,ClockEvent ,IRQState };use migration::{self, prelude::*}; -use qom::{prelude::*,ObjectImpl ,Owned ,ParentField ,ParentInit }; +use qom::prelude::*;use system::prelude::*;use util::prelude::*;
rust/hw/core/src/qdev.rs +1 -1
@@ -12,7 +12,7 @@use chardev::Chardev ;use common::{callbacks::FnCall ,Opaque };use migration::{impl_vmstate_c_struct,VMStateDescription }; -use qom::{prelude::*,ObjectClass ,ObjectImpl ,Owned ,ParentInit }; +use qom::{prelude::*,ObjectClass };use util::{Error ,Result };pub use crate ::bindings::{ClockEvent ,DeviceClass ,Property ,ResetType };
rust/hw/core/src/sysbus.rs +1 -1
@@ -8,7 +8,7 @@pub use bindings::SysBusDeviceClass ;use common::Opaque ; -use qom::{prelude::*,Owned }; +use qom::prelude::*;use system::MemoryRegion ;use util::{Error ,Result };
rust/hw/core/tests/tests.rs +1 -1
@@ -7,7 +7,7 @@use bql::prelude::*;use hwcore::prelude::*;use migration::prelude::*; -use qom::{prelude::*,ObjectImpl ,ParentField }; +use qom::prelude::*;use util::bindings::{module_call_init, module_init_type};// Test that macros can compile.
rust/hw/timer/hpet/src/device.rs +1 -1
@@ -14,7 +14,7 @@use common::prelude::*;use hwcore::prelude::*;use migration::{self, prelude::*}; -use qom::{prelude::*,ObjectImpl ,ParentField ,ParentInit }; +use qom::prelude::*;use system::{ bindings::{address_space_memory, address_space_stl_le},MEMTXATTRS_UNSPECIFIED ,
rust/qom/src/prelude.rs +4 -0
@@ -6,7 +6,11 @@pub use crate ::qom::ObjectCast ;pub use crate ::qom::ObjectClassMethods ;pub use crate ::qom::ObjectDeref ; +pub use crate ::qom::ObjectImpl ;pub use crate ::qom::ObjectMethods ;pub use crate ::qom::ObjectType ; +pub use crate ::qom::Owned ; +pub use crate ::qom::ParentField ; +pub use crate ::qom::ParentInit ;pub use crate ::qom_isa;
Signature · 1 line
2.51.1
Hi
Quoted 11 lines · Paolo Bonzini
This series adds a prelude module to all crates, so that it becomes possible to import from each crate with either of the following
use XYZ::prelude::*; use XYZ::{self, prelude::*};The latter is used for items that have a "too common" name to be put in the prelude:
util::Error,util::Result,migration::Infallible.
In my experience, "preludes" are not so ubiquitous (except the std library). The use of wildcard imports is not encouraged, and may result in conflicts. Also, it's often subjective what you put there or not.
I don't think we should encourage it, having explicit import of what is used is often clearer.
Quoted 59 lines
Thanks,
Paolo
Paolo Bonzini (8):
util: add prelude
common: add prelude
hwcore: add prelude
migration: add prelude
chardev: add prelude
bql: add prelude
system: add prelude
qom: add more to the preluderust/bql/meson.build | 1 + rust/bql/src/lib.rs | 5 +++++ rust/bql/src/prelude.rs | 4 ++++ rust/chardev/meson.build | 1 + rust/chardev/src/chardev.rs | 2 +- rust/chardev/src/lib.rs | 5 +++++ rust/chardev/src/prelude.rs | 5 +++++ rust/common/meson.build | 13 +------------ rust/common/src/lib.rs | 5 +++++ rust/common/src/prelude.rs | 9 +++++++++ rust/hw/char/pl011/src/device.rs | 22 ++++++++-------------- rust/hw/core/meson.build | 1 + rust/hw/core/src/lib.rs | 5 +++++ rust/hw/core/src/prelude.rs | 14 ++++++++++++++ rust/hw/core/src/qdev.rs | 2 +- rust/hw/core/src/sysbus.rs | 2 +- rust/hw/core/tests/tests.rs | 8 ++++---- rust/hw/timer/hpet/src/device.rs | 26 +++++++++----------------- rust/hw/timer/hpet/src/fw_cfg.rs | 2 +- rust/migration/meson.build | 1 + rust/migration/src/lib.rs | 5 +++++ rust/migration/src/migratable.rs | 2 +- rust/migration/src/prelude.rs | 19 +++++++++++++++++++ rust/qom/src/prelude.rs | 4 ++++ rust/system/meson.build | 1 + rust/system/src/lib.rs | 5 +++++ rust/system/src/prelude.rs | 8 ++++++++ rust/tests/tests/vmstate_tests.rs | 7 +++---- rust/util/meson.build | 1 + rust/util/src/lib.rs | 5 +++++ rust/util/src/prelude.rs | 11 +++++++++++ 31 files changed, 145 insertions(+), 56 deletions(-) create mode 100644 rust/bql/src/prelude.rs create mode 100644 rust/chardev/src/prelude.rs create mode 100644 rust/common/src/prelude.rs create mode 100644 rust/hw/core/src/prelude.rs create mode 100644 rust/migration/src/prelude.rs create mode 100644 rust/system/src/prelude.rs create mode 100644 rust/util/src/prelude.rs--
2.51.1
Signature · 1 line
Marc-André Lureau
Quoted 12 lines · Marc-André Lureau
Hi
Quoted 8 lines · Paolo Bonzini
This series adds a prelude module to all crates, so that it becomes possible to import from each crate with either of the following
use XYZ::prelude::*; use XYZ::{self, prelude::*};
Quoted 7 lines
Quoted 3 lines
The latter is used for items that have a "too common" name to be put in the prelude:
util::Error,util::Result,migration::Infallible.In my experience, "preludes" are not so ubiquitous (except the std library). The use of wildcard imports is not encouraged, and may result in conflicts.
Yes, I agree with this. On the other hand, we already have lists of imports that are two-three lines long and I'm afraid that people would prefer to just import migration::* for example. While we can block them with -Dclippy::wildcard_imports, the problem is real.
So I was a bit undecided and went looking for examples of crates that do have a prelude. I first noticed that bevy has a similar system, with each of its crates providing a prelude. IMO QEMU is a similar case to bevy, where most of the code will use structs from the QEMU crates more than std, so it makes sense to have them. Futures and Tokio also have a prelude, and they also fit the idea of crates that provide a programming environment.
Preludes let you import libraries with a level of detail similar to C includes. For simple library crates that offer two-three types it's not necessary to have one, but as the complexity and number of crates increase, they provide more clarity.
Another thing to notice is that the QOM bindings work a lot with extension traits implemented on all types. There should be no need for users to know the difference between DeviceState, DeviceMethods and DeviceClassMethods. Preludes help with that.
Quoted 1 line
Also, it's often subjective what you put there or not.
Right - for now I just defined what should *never* be in a prelude:
the rule was simply "do not include anything that can conflict with std". This should not be that bad, otherwise we'd have similar problems with C includes as mentioned above.
Another thing that should not be in the preludes without extremely good reasons is enum variants; Ok/Err and Some/None are the only exceptions in the standard library prelude, and they should probably remain the only ones.
Paolo