Skip to content

Commit e97f718

Browse files
authored
Merge pull request #162 from rcore-os/zerocopy
Update to zerocopy 0.8.7.
2 parents c0807ba + 72edbcd commit e97f718

File tree

15 files changed

+144
-122
lines changed

15 files changed

+144
-122
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ log = "0.4.22"
1919
bitflags = "2.6.0"
2020
enumn = "0.1.14"
2121
embedded-io = { version = "0.6.1", optional = true }
22-
zerocopy = { version = "0.7.35", features = ["derive"] }
22+
zerocopy = { version = "0.8.7", features = ["derive"] }
2323

2424
[features]
2525
default = ["alloc", "embedded-io"]
2626
alloc = ["zerocopy/alloc"]
2727
embedded-io = ["dep:embedded-io"]
2828

2929
[dev-dependencies]
30-
zerocopy = { version = "0.7.35", features = ["alloc"] }
30+
zerocopy = { version = "0.8.7", features = ["alloc"] }

src/device/blk.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::volatile::{volread, Volatile};
77
use crate::{Error, Result};
88
use bitflags::bitflags;
99
use log::info;
10-
use zerocopy::{AsBytes, FromBytes, FromZeroes};
10+
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
1111

1212
const QUEUE: u16 = 0;
1313
const QUEUE_SIZE: u16 = 16;
@@ -111,7 +111,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
111111
let mut resp = BlkResp::default();
112112
self.queue.add_notify_wait_pop(
113113
&[request.as_bytes()],
114-
&mut [resp.as_bytes_mut()],
114+
&mut [resp.as_mut_bytes()],
115115
&mut self.transport,
116116
)?;
117117
resp.status.into()
@@ -122,7 +122,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
122122
let mut resp = BlkResp::default();
123123
self.queue.add_notify_wait_pop(
124124
&[request.as_bytes()],
125-
&mut [data, resp.as_bytes_mut()],
125+
&mut [data, resp.as_mut_bytes()],
126126
&mut self.transport,
127127
)?;
128128
resp.status.into()
@@ -133,7 +133,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
133133
let mut resp = BlkResp::default();
134134
self.queue.add_notify_wait_pop(
135135
&[request.as_bytes(), data],
136-
&mut [resp.as_bytes_mut()],
136+
&mut [resp.as_mut_bytes()],
137137
&mut self.transport,
138138
)?;
139139
resp.status.into()
@@ -261,7 +261,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
261261
};
262262
let token = self
263263
.queue
264-
.add(&[req.as_bytes()], &mut [buf, resp.as_bytes_mut()])?;
264+
.add(&[req.as_bytes()], &mut [buf, resp.as_mut_bytes()])?;
265265
if self.queue.should_notify() {
266266
self.transport.notify(QUEUE);
267267
}
@@ -282,7 +282,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
282282
resp: &mut BlkResp,
283283
) -> Result<()> {
284284
self.queue
285-
.pop_used(token, &[req.as_bytes()], &mut [buf, resp.as_bytes_mut()])?;
285+
.pop_used(token, &[req.as_bytes()], &mut [buf, resp.as_mut_bytes()])?;
286286
resp.status.into()
287287
}
288288

@@ -343,7 +343,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
343343
};
344344
let token = self
345345
.queue
346-
.add(&[req.as_bytes(), buf], &mut [resp.as_bytes_mut()])?;
346+
.add(&[req.as_bytes(), buf], &mut [resp.as_mut_bytes()])?;
347347
if self.queue.should_notify() {
348348
self.transport.notify(QUEUE);
349349
}
@@ -364,7 +364,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
364364
resp: &mut BlkResp,
365365
) -> Result<()> {
366366
self.queue
367-
.pop_used(token, &[req.as_bytes(), buf], &mut [resp.as_bytes_mut()])?;
367+
.pop_used(token, &[req.as_bytes(), buf], &mut [resp.as_mut_bytes()])?;
368368
resp.status.into()
369369
}
370370

@@ -410,7 +410,7 @@ struct BlkConfig {
410410

411411
/// A VirtIO block device request.
412412
#[repr(C)]
413-
#[derive(AsBytes, Debug)]
413+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
414414
pub struct BlkReq {
415415
type_: ReqType,
416416
reserved: u32,
@@ -429,7 +429,7 @@ impl Default for BlkReq {
429429

430430
/// Response of a VirtIOBlk request.
431431
#[repr(C)]
432-
#[derive(AsBytes, Debug, FromBytes, FromZeroes)]
432+
#[derive(Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
433433
pub struct BlkResp {
434434
status: RespStatus,
435435
}
@@ -442,7 +442,7 @@ impl BlkResp {
442442
}
443443

444444
#[repr(u32)]
445-
#[derive(AsBytes, Debug)]
445+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
446446
enum ReqType {
447447
In = 0,
448448
Out = 1,
@@ -456,7 +456,7 @@ enum ReqType {
456456

457457
/// Status of a VirtIOBlk request.
458458
#[repr(transparent)]
459-
#[derive(AsBytes, Copy, Clone, Debug, Eq, FromBytes, FromZeroes, PartialEq)]
459+
#[derive(Copy, Clone, Debug, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
460460
pub struct RespStatus(u8);
461461

462462
impl RespStatus {

src/device/gpu.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{pages, Error, Result, PAGE_SIZE};
88
use alloc::boxed::Box;
99
use bitflags::bitflags;
1010
use log::info;
11-
use zerocopy::{AsBytes, FromBytes, FromZeroes};
11+
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};
1212

1313
const QUEUE_SIZE: u16 = 2;
1414
const SUPPORTED_FEATURES: Features = Features::RING_EVENT_IDX.union(Features::RING_INDIRECT_DESC);
@@ -66,8 +66,8 @@ impl<H: Hal, T: Transport> VirtIOGpu<H, T> {
6666
negotiated_features.contains(Features::RING_EVENT_IDX),
6767
)?;
6868

69-
let queue_buf_send = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);
70-
let queue_buf_recv = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);
69+
let queue_buf_send = FromZeros::new_box_zeroed_with_elems(PAGE_SIZE).unwrap();
70+
let queue_buf_recv = FromZeros::new_box_zeroed_with_elems(PAGE_SIZE).unwrap();
7171

7272
transport.finish_init();
7373

@@ -173,18 +173,18 @@ impl<H: Hal, T: Transport> VirtIOGpu<H, T> {
173173
}
174174

175175
/// Send a request to the device and block for a response.
176-
fn request<Req: AsBytes, Rsp: FromBytes>(&mut self, req: Req) -> Result<Rsp> {
176+
fn request<Req: IntoBytes + Immutable, Rsp: FromBytes>(&mut self, req: Req) -> Result<Rsp> {
177177
req.write_to_prefix(&mut self.queue_buf_send).unwrap();
178178
self.control_queue.add_notify_wait_pop(
179179
&[&self.queue_buf_send],
180180
&mut [&mut self.queue_buf_recv],
181181
&mut self.transport,
182182
)?;
183-
Ok(Rsp::read_from_prefix(&self.queue_buf_recv).unwrap())
183+
Ok(Rsp::read_from_prefix(&self.queue_buf_recv).unwrap().0)
184184
}
185185

186186
/// Send a mouse cursor operation request to the device and block for a response.
187-
fn cursor_request<Req: AsBytes>(&mut self, req: Req) -> Result {
187+
fn cursor_request<Req: IntoBytes + Immutable>(&mut self, req: Req) -> Result {
188188
req.write_to_prefix(&mut self.queue_buf_send).unwrap();
189189
self.cursor_queue.add_notify_wait_pop(
190190
&[&self.queue_buf_send],
@@ -338,7 +338,7 @@ bitflags! {
338338
}
339339

340340
#[repr(transparent)]
341-
#[derive(AsBytes, Clone, Copy, Debug, Eq, PartialEq, FromBytes, FromZeroes)]
341+
#[derive(Clone, Copy, Debug, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
342342
struct Command(u32);
343343

344344
impl Command {
@@ -371,7 +371,7 @@ impl Command {
371371
const GPU_FLAG_FENCE: u32 = 1 << 0;
372372

373373
#[repr(C)]
374-
#[derive(AsBytes, Debug, Clone, Copy, FromBytes, FromZeroes)]
374+
#[derive(Debug, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout)]
375375
struct CtrlHeader {
376376
hdr_type: Command,
377377
flags: u32,
@@ -402,7 +402,7 @@ impl CtrlHeader {
402402
}
403403

404404
#[repr(C)]
405-
#[derive(AsBytes, Debug, Copy, Clone, Default, FromBytes, FromZeroes)]
405+
#[derive(Debug, Copy, Clone, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
406406
struct Rect {
407407
x: u32,
408408
y: u32,
@@ -411,7 +411,7 @@ struct Rect {
411411
}
412412

413413
#[repr(C)]
414-
#[derive(Debug, FromBytes, FromZeroes)]
414+
#[derive(Debug, FromBytes, Immutable, KnownLayout)]
415415
struct RespDisplayInfo {
416416
header: CtrlHeader,
417417
rect: Rect,
@@ -420,7 +420,7 @@ struct RespDisplayInfo {
420420
}
421421

422422
#[repr(C)]
423-
#[derive(AsBytes, Debug)]
423+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
424424
struct ResourceCreate2D {
425425
header: CtrlHeader,
426426
resource_id: u32,
@@ -430,13 +430,13 @@ struct ResourceCreate2D {
430430
}
431431

432432
#[repr(u32)]
433-
#[derive(AsBytes, Debug)]
433+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
434434
enum Format {
435435
B8G8R8A8UNORM = 1,
436436
}
437437

438438
#[repr(C)]
439-
#[derive(AsBytes, Debug)]
439+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
440440
struct ResourceAttachBacking {
441441
header: CtrlHeader,
442442
resource_id: u32,
@@ -447,7 +447,7 @@ struct ResourceAttachBacking {
447447
}
448448

449449
#[repr(C)]
450-
#[derive(AsBytes, Debug)]
450+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
451451
struct SetScanout {
452452
header: CtrlHeader,
453453
rect: Rect,
@@ -456,7 +456,7 @@ struct SetScanout {
456456
}
457457

458458
#[repr(C)]
459-
#[derive(AsBytes, Debug)]
459+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
460460
struct TransferToHost2D {
461461
header: CtrlHeader,
462462
rect: Rect,
@@ -466,7 +466,7 @@ struct TransferToHost2D {
466466
}
467467

468468
#[repr(C)]
469-
#[derive(AsBytes, Debug)]
469+
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
470470
struct ResourceFlush {
471471
header: CtrlHeader,
472472
rect: Rect,
@@ -475,7 +475,7 @@ struct ResourceFlush {
475475
}
476476

477477
#[repr(C)]
478-
#[derive(AsBytes, Debug, Clone, Copy)]
478+
#[derive(Copy, Clone, Debug, Immutable, IntoBytes, KnownLayout)]
479479
struct CursorPos {
480480
scanout_id: u32,
481481
x: u32,
@@ -484,7 +484,7 @@ struct CursorPos {
484484
}
485485

486486
#[repr(C)]
487-
#[derive(AsBytes, Debug, Clone, Copy)]
487+
#[derive(Copy, Clone, Debug, Immutable, IntoBytes, KnownLayout)]
488488
struct UpdateCursor {
489489
header: CtrlHeader,
490490
pos: CursorPos,

src/device/input.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use alloc::{boxed::Box, string::String};
1010
use core::cmp::min;
1111
use core::mem::size_of;
1212
use core::ptr::{addr_of, NonNull};
13-
use zerocopy::{AsBytes, FromBytes, FromZeroes};
13+
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};
1414

1515
/// Virtual human interface devices such as keyboards, mice and tablets.
1616
///
@@ -48,7 +48,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
4848
)?;
4949
for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
5050
// Safe because the buffer lasts as long as the queue.
51-
let token = unsafe { event_queue.add(&[], &mut [event.as_bytes_mut()])? };
51+
let token = unsafe { event_queue.add(&[], &mut [event.as_mut_bytes()])? };
5252
assert_eq!(token, i as u16);
5353
}
5454
if event_queue.should_notify() {
@@ -79,13 +79,13 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
7979
// is still valid.
8080
unsafe {
8181
self.event_queue
82-
.pop_used(token, &[], &mut [event.as_bytes_mut()])
82+
.pop_used(token, &[], &mut [event.as_mut_bytes()])
8383
.ok()?;
8484
}
8585
let event_saved = *event;
8686
// requeue
8787
// Safe because buffer lasts as long as the queue.
88-
if let Ok(new_token) = unsafe { self.event_queue.add(&[], &mut [event.as_bytes_mut()]) }
88+
if let Ok(new_token) = unsafe { self.event_queue.add(&[], &mut [event.as_mut_bytes()]) }
8989
{
9090
// This only works because nothing happen between `pop_used` and `add` that affects
9191
// the list of free descriptors in the queue, so `add` reuses the descriptor which
@@ -137,7 +137,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
137137
if size > CONFIG_DATA_MAX_LENGTH {
138138
return Err(Error::IoError);
139139
}
140-
let mut buf = u8::new_box_slice_zeroed(size);
140+
let mut buf = <[u8]>::new_box_zeroed_with_elems(size).unwrap();
141141
for i in 0..size {
142142
buf[i] = addr_of!((*self.config.as_ptr()).data[i]).vread();
143143
}
@@ -172,7 +172,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
172172
/// Queries and returns the ID information of the device.
173173
pub fn ids(&mut self) -> Result<DevIDs, Error> {
174174
let mut ids = DevIDs::default();
175-
let size = self.query_config_select(InputConfigSelect::IdDevids, 0, ids.as_bytes_mut());
175+
let size = self.query_config_select(InputConfigSelect::IdDevids, 0, ids.as_mut_bytes());
176176
if usize::from(size) == size_of::<DevIDs>() {
177177
Ok(ids)
178178
} else {
@@ -195,7 +195,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
195195
/// Queries and returns information about the given axis of the device.
196196
pub fn abs_info(&mut self, axis: u8) -> Result<AbsInfo, Error> {
197197
let mut info = AbsInfo::default();
198-
let size = self.query_config_select(InputConfigSelect::AbsInfo, axis, info.as_bytes_mut());
198+
let size = self.query_config_select(InputConfigSelect::AbsInfo, axis, info.as_mut_bytes());
199199
if usize::from(size) == size_of::<AbsInfo>() {
200200
Ok(info)
201201
} else {
@@ -263,7 +263,7 @@ struct Config {
263263

264264
/// Information about an axis of an input device, typically a joystick.
265265
#[repr(C)]
266-
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
266+
#[derive(Clone, Debug, Default, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
267267
pub struct AbsInfo {
268268
/// The minimum value for the axis.
269269
pub min: u32,
@@ -279,7 +279,7 @@ pub struct AbsInfo {
279279

280280
/// The identifiers of a VirtIO input device.
281281
#[repr(C)]
282-
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
282+
#[derive(Clone, Debug, Default, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
283283
pub struct DevIDs {
284284
/// The bustype identifier.
285285
pub bustype: u16,
@@ -294,7 +294,7 @@ pub struct DevIDs {
294294
/// Both queues use the same `virtio_input_event` struct. `type`, `code` and `value`
295295
/// are filled according to the Linux input layer (evdev) interface.
296296
#[repr(C)]
297-
#[derive(AsBytes, Clone, Copy, Debug, Default, FromBytes, FromZeroes)]
297+
#[derive(Clone, Copy, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
298298
pub struct InputEvent {
299299
/// Event type.
300300
pub event_type: u16,

src/device/net/dev_raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::transport::Transport;
66
use crate::volatile::volread;
77
use crate::{Error, Result};
88
use log::{debug, info, warn};
9-
use zerocopy::AsBytes;
9+
use zerocopy::IntoBytes;
1010

1111
/// Raw driver for a VirtIO network device.
1212
///

0 commit comments

Comments
 (0)