Skip to content

Commit

Permalink
vhost-user-backend: use Cow<'static, str>
Browse files Browse the repository at this point in the history
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 <manos.pitsidianakis@linaro.org>
  • Loading branch information
epilys committed Oct 7, 2023
1 parent 8a569c0 commit cd15176
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions vhost-user-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[macro_use]
extern crate log;

use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::path::Path;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -81,7 +82,7 @@ pub type Result<T> = std::result::Result<T, Error>;
/// This structure is the public API the backend is allowed to interact with in order to run
/// a fully functional vhost-user daemon.
pub struct VhostUserDaemon<T: VhostUserBackend> {
name: String,
name: Cow<'static, str>,
handler: Arc<Mutex<VhostUserHandler<T>>>,
main_thread: Option<thread::JoinHandle<Result<()>>>,
}
Expand All @@ -98,7 +99,7 @@ where
/// registered event. Those events can be vring events or custom events from the backend,
/// but they get to be registered later during the sequence.
pub fn new(
name: String,
name: Cow<'static, str>,
backend: T,
atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<T::Bitmap>>,
) -> Result<Self> {
Expand All @@ -124,7 +125,7 @@ where
mut handler: BackendReqHandler<Mutex<VhostUserHandler<T>>>,
) -> Result<()> {
let handle = thread::Builder::new()
.name(self.name.clone())
.name(self.name.to_string())
.spawn(move || loop {
handler.handle_request().map_err(Error::HandleRequest)?;
})
Expand Down Expand Up @@ -253,7 +254,7 @@ mod tests {
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();

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

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

Expand Down
2 changes: 1 addition & 1 deletion vhost-user-backend/tests/vhost-user-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn vhost_user_client(path: &Path, barrier: Arc<Barrier>) {
fn vhost_user_server(cb: fn(&Path, Arc<Barrier>)) {
let mem = GuestMemoryAtomic::new(GuestMemoryMmap::<()>::new());
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();

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

0 comments on commit cd15176

Please sign in to comment.