Skip to content

Commit cd15176

Browse files
committed
vhost-user-backend: use Cow<'static, str>
Change the name's type `String` to `Cow<'static, str>`. This allows the library user to prevent string allocation from static strings, which is how most crates use it. This is an API breaking change. Signed-off-by: Manos Pitsidianakis <[email protected]>
1 parent 8a569c0 commit cd15176

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

vhost-user-backend/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[macro_use]
99
extern crate log;
1010

11+
use std::borrow::Cow;
1112
use std::fmt::{Display, Formatter};
1213
use std::path::Path;
1314
use std::sync::{Arc, Mutex};
@@ -81,7 +82,7 @@ pub type Result<T> = std::result::Result<T, Error>;
8182
/// This structure is the public API the backend is allowed to interact with in order to run
8283
/// a fully functional vhost-user daemon.
8384
pub struct VhostUserDaemon<T: VhostUserBackend> {
84-
name: String,
85+
name: Cow<'static, str>,
8586
handler: Arc<Mutex<VhostUserHandler<T>>>,
8687
main_thread: Option<thread::JoinHandle<Result<()>>>,
8788
}
@@ -98,7 +99,7 @@ where
9899
/// registered event. Those events can be vring events or custom events from the backend,
99100
/// but they get to be registered later during the sequence.
100101
pub fn new(
101-
name: String,
102+
name: Cow<'static, str>,
102103
backend: T,
103104
atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<T::Bitmap>>,
104105
) -> Result<Self> {
@@ -124,7 +125,7 @@ where
124125
mut handler: BackendReqHandler<Mutex<VhostUserHandler<T>>>,
125126
) -> Result<()> {
126127
let handle = thread::Builder::new()
127-
.name(self.name.clone())
128+
.name(self.name.to_string())
128129
.spawn(move || loop {
129130
handler.handle_request().map_err(Error::HandleRequest)?;
130131
})
@@ -253,7 +254,7 @@ mod tests {
253254
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
254255
);
255256
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
256-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
257+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
257258

258259
let handlers = daemon.get_epoll_handlers();
259260
assert_eq!(handlers.len(), 2);
@@ -286,7 +287,7 @@ mod tests {
286287
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
287288
);
288289
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
289-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
290+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
290291

291292
let handlers = daemon.get_epoll_handlers();
292293
assert_eq!(handlers.len(), 2);
@@ -321,7 +322,7 @@ mod tests {
321322
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
322323
);
323324
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
324-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend.clone(), mem).unwrap();
325+
let mut daemon = VhostUserDaemon::new("test".into(), backend.clone(), mem).unwrap();
325326
let tmpdir = tempfile::tempdir().unwrap();
326327
let socket_path = tmpdir.path().join("socket");
327328

vhost-user-backend/tests/vhost-user-server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn vhost_user_client(path: &Path, barrier: Arc<Barrier>) {
220220
fn vhost_user_server(cb: fn(&Path, Arc<Barrier>)) {
221221
let mem = GuestMemoryAtomic::new(GuestMemoryMmap::<()>::new());
222222
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
223-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
223+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
224224

225225
let barrier = Arc::new(Barrier::new(2));
226226
let tmpdir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)