Skip to content

Commit 14de73e

Browse files
committed
Removing use of static mut reference
1 parent edafa99 commit 14de73e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ pub mod endpoint;
134134
/// To implement USB support for your own project, the required code is usually as follows:
135135
///
136136
/// ``` ignore
137+
/// use core::cell::UnsafeCell;
137138
/// use usb_device::prelude::*;
138139
/// use usb_serial; // example class crate (not included)
139140
///
140-
/// static mut CONTROL_BUFFER: [u8; 128] = [0; 128];
141+
/// static mut CONTROL_BUFFER: UnsafeCell<[u8; 128]> = UnsafeCell::new([0; 128]);
141142
///
142143
/// // Create the device-specific USB peripheral driver. The exact name and arguments are device
143144
/// // specific, so check the documentation for your device driver crate.
@@ -153,7 +154,7 @@ pub mod endpoint;
153154
/// // pair. Additional builder arguments can specify parameters such as device class code or
154155
/// // product name. If using an existing class, remember to check the class crate documentation
155156
/// // for correct values.
156-
/// let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x5824, 0x27dd), unsafe { &mut CONTROL_BUFFER })
157+
/// let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x5824, 0x27dd), unsafe { CONTROL_BUFFER.get_mut() })
157158
/// .strings(&[StringDescriptors::new(LangID::EN)
158159
/// .product("Serial port")])
159160
/// .expect("Failed to set strings")

src/test_class.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![allow(missing_docs)]
22

33
use crate::class_prelude::*;
4-
use crate::descriptor::lang_id::LangID;
54
use crate::device::{StringDescriptors, UsbDevice, UsbDeviceBuilder, UsbVidPid};
65
use crate::Result;
6+
use core::cell::UnsafeCell;
77
use core::cmp;
88

99
#[cfg(feature = "test-class-high-speed")]
@@ -22,7 +22,7 @@ mod sizes {
2222
pub const INTERRUPT_ENDPOINT: u16 = 31;
2323
}
2424

25-
static mut CONTROL_BUFFER: [u8; 256] = [0; 256];
25+
static mut CONTROL_BUFFER: UnsafeCell<[u8; 256]> = UnsafeCell::new([0; 256]);
2626

2727
/// Test USB class for testing USB driver implementations. Supports various endpoint types and
2828
/// requests for testing USB peripheral drivers on actual hardware.
@@ -114,14 +114,16 @@ impl<B: UsbBus> TestClass<'_, B> {
114114
&self,
115115
usb_bus: &'a UsbBusAllocator<B>,
116116
) -> UsbDeviceBuilder<'a, B> {
117-
UsbDeviceBuilder::new(usb_bus, UsbVidPid(VID, PID), unsafe { &mut CONTROL_BUFFER })
118-
.strings(&[StringDescriptors::default()
119-
.manufacturer(MANUFACTURER)
120-
.product(PRODUCT)
121-
.serial_number(SERIAL_NUMBER)])
122-
.unwrap()
123-
.max_packet_size_0(sizes::CONTROL_ENDPOINT)
124-
.unwrap()
117+
UsbDeviceBuilder::new(usb_bus, UsbVidPid(VID, PID), unsafe {
118+
CONTROL_BUFFER.get_mut()
119+
})
120+
.strings(&[StringDescriptors::default()
121+
.manufacturer(MANUFACTURER)
122+
.product(PRODUCT)
123+
.serial_number(SERIAL_NUMBER)])
124+
.unwrap()
125+
.max_packet_size_0(sizes::CONTROL_ENDPOINT)
126+
.unwrap()
125127
}
126128

127129
/// Must be called after polling the UsbDevice.

0 commit comments

Comments
 (0)