Skip to content

Commit

Permalink
Require T: Immutable on write_config_space.
Browse files Browse the repository at this point in the history
This is necessary for the relevant methods on IntoBytes.
  • Loading branch information
qwandor committed Nov 26, 2024
1 parent 54e8c9d commit e98c806
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::{
mem::{align_of, size_of},
ptr::NonNull,
};
use zerocopy::{FromBytes, IntoBytes};
use zerocopy::{FromBytes, Immutable, IntoBytes};

const MAGIC_VALUE: u32 = 0x7472_6976;
pub(crate) const LEGACY_VERSION: u32 = 1;
Expand Down Expand Up @@ -519,7 +519,11 @@ impl Transport for MmioTransport {
}
}

fn write_config_space<T: IntoBytes>(&mut self, offset: usize, value: T) -> Result<(), Error> {
fn write_config_space<T: IntoBytes + Immutable>(
&mut self,
offset: usize,
value: T,
) -> Result<(), Error> {
assert!(align_of::<T>() <= 4,
"Driver expected config space alignment of {} bytes, but VirtIO only guarantees 4 byte alignment.",
align_of::<T>());
Expand Down
8 changes: 6 additions & 2 deletions src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitflags::{bitflags, Flags};
use core::{fmt::Debug, ops::BitAnd};
use log::debug;
pub use some::SomeTransport;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy::{FromBytes, Immutable, IntoBytes};

/// A VirtIO transport layer.
pub trait Transport {
Expand Down Expand Up @@ -105,7 +105,11 @@ pub trait Transport {
fn read_config_space<T: FromBytes>(&self, offset: usize) -> Result<T>;

/// Writes a value to the device config space.
fn write_config_space<T: IntoBytes>(&mut self, offset: usize, value: T) -> Result<()>;
fn write_config_space<T: IntoBytes + Immutable>(
&mut self,
offset: usize,
value: T,
) -> Result<()>;
}

bitflags! {
Expand Down
8 changes: 6 additions & 2 deletions src/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::{
mem::{align_of, size_of},
ptr::{addr_of_mut, NonNull},
};
use zerocopy::{FromBytes, IntoBytes};
use zerocopy::{FromBytes, Immutable, IntoBytes};

/// The PCI vendor ID for VirtIO devices.
const VIRTIO_VENDOR_ID: u16 = 0x1af4;
Expand Down Expand Up @@ -346,7 +346,11 @@ impl Transport for PciTransport {
}
}

fn write_config_space<T: IntoBytes>(&mut self, offset: usize, value: T) -> Result<(), Error> {
fn write_config_space<T: IntoBytes + Immutable>(
&mut self,
offset: usize,
value: T,
) -> Result<(), Error> {
assert!(align_of::<T>() <= 4,
"Driver expected config space alignment of {} bytes, but VirtIO only guarantees 4 byte alignment.",
align_of::<T>());
Expand Down
8 changes: 6 additions & 2 deletions src/transport/some.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zerocopy::{FromBytes, IntoBytes};
use zerocopy::{FromBytes, Immutable, IntoBytes};

use super::{mmio::MmioTransport, pci::PciTransport, DeviceStatus, DeviceType, Transport};
use crate::{PhysAddr, Result};
Expand Down Expand Up @@ -130,7 +130,11 @@ impl Transport for SomeTransport {
}
}

fn write_config_space<T: IntoBytes>(&mut self, offset: usize, value: T) -> Result<()> {
fn write_config_space<T: IntoBytes + Immutable>(
&mut self,
offset: usize,
value: T,
) -> Result<()> {
match self {
Self::Mmio(mmio) => mmio.write_config_space(offset, value),
Self::Pci(pci) => pci.write_config_space(offset, value),
Expand Down

0 comments on commit e98c806

Please sign in to comment.