On Tue, Oct 28, 2025 at 12:18 PM Chen Miao <[email protected]> wrote:
>
> On 10/28/2025 6:45 PM, Paolo Bonzini wrote:
> > On Tue, Oct 28, 2025 at 11:18 AM chenmiao <[email protected]> wrote:
> >> During the implementation process, we found that the current two paradigms 
> >> in
> >> Rust — bindings and impl — are extremely complex and lack comprehensive
> >> documentation. There is no clear explanation as to why Bus and Device 
> >> models
> >> need to be implemented using different approaches.
> > I don't think they need to be implemented using different approaches.
> > The difference between the two is that:
> >
> > - the currently implemented devices do not expose any bus, they stay
> > on a bus. This means the bus is never a child of the device
> >
> > - the currently implemented buses are all in C code, whereas there are
> > devices implemented in Rust.
> >
> > I agree that the Rust-to-C bridge code is complex, but it does have
> > documentation, much more so than the C versions in fact.  If there are
> > specific aspects of the documentation that you would like to see
> > improved, you can help by explaining what problems and sources of
> > confusion you encountered.
>
> That makes sense to me. Since buses are mainly implemented in C, the current
> focus of Rust is on porting the device layer. Rust-implemented devices are
> consumers and do not create any child objects.
>
> I think our previous confusion stemmed from the assumption that Rust was
> porting the entire hierarchy, when in fact Rust is currently only implementing
> the device layer.
>
> >> +/// A safe wrapper around [`bindings::I2CBus`].
> >> +#[repr(transparent)]
> >> +#[derive(Debug, common::Wrapper)]
> >> +pub struct I2CBus(Opaque<bindings::I2CBus>);
> >> +
> >> +unsafe impl Send for I2CBus {}
> >> +unsafe impl Sync for I2CBus {}
> >> +
> >> +unsafe impl ObjectType for I2CBus {
> >> +    type Class = BusClass;
> >> +    const TYPE_NAME: &'static CStr =
> >> +        unsafe { 
> >> CStr::from_bytes_with_nul_unchecked(bindings::TYPE_I2C_BUS) };
> >> +}
> >> +
> >> +qom_isa!(I2CBus: BusState, Object);
> >> +
> >> +// TODO: add virtual methods
> >> +pub trait I2CBusImpl: DeviceImpl + IsA<I2CBus> {}
> >> +/// Trait for methods of [`I2CBus`] and its subclasses.
> >> +pub trait I2CBusMethods: ObjectDeref
> >> +where
> >> +    Self::Target: IsA<I2CBus>
> >
> > There are no virtual methods, and therefore I2CBus does not have
> > subclasses.  Therefore you don't need these traits and you can
> > implement the functions directly on I2CBus.
>
> For this part, we directly referred to the implementation of SysBus.

I see. The difference is that:

- in sysbus there are virtual methods, but Rust does not let you override them

- in I2C there are no virtual methods added (and in fact you have
"type Class = BusClass").

> I'll revise all the points you have raised later.

Thank you!

Paolo


Reply via email to