Skip to content

Commit 977ae82

Browse files
committed
ch: use TypeId trait for key type in FanoutWriter
Signed-off-by: Stefan Kober <stefan.kober@cyberus-technology.de>
1 parent ce3e862 commit 977ae82

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

vmm/src/serial_manager.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44
//
55

6+
use std::any::TypeId;
67
use std::collections::HashMap;
78
use std::fs::File;
89
use std::io::{Read, Write};
@@ -115,7 +116,7 @@ impl From<u64> for EpollDispatch {
115116
/// a TCP socket and a file.
116117
#[derive(Clone)]
117118
pub struct FanoutWriter {
118-
writers: Arc<Mutex<HashMap<String, Box<dyn Write + Send>>>>,
119+
writers: Arc<Mutex<HashMap<TypeId, Box<dyn Write + Send>>>>,
119120
}
120121

121122
impl FanoutWriter {
@@ -125,14 +126,14 @@ impl FanoutWriter {
125126
}
126127
}
127128

128-
pub fn add_writer<W: Write + Send + 'static>(&self, key: String, writer: W) {
129+
pub fn add_writer<W: Write + Send + 'static>(&self, writer: W) {
129130
let mut writers = self.writers.lock().unwrap();
130-
writers.insert(key, Box::new(writer));
131+
writers.insert(TypeId::of::<W>(), Box::new(writer));
131132
}
132133

133-
pub fn remove_writer(&self, key: &str) -> Option<Box<dyn Write + Send>> {
134+
pub fn remove_writer(&self, id: TypeId) -> Option<Box<dyn Write + Send>> {
134135
let mut writers = self.writers.lock().unwrap();
135-
writers.remove(key)
136+
writers.remove(&id)
136137
}
137138
}
138139

@@ -336,7 +337,7 @@ impl SerialManager {
336337
let write_distributor = FanoutWriter::new();
337338

338339
if let ConsoleOutput::Tcp(_, Some(f)) = &in_file {
339-
write_distributor.add_writer("file".into(), f.clone());
340+
write_distributor.add_writer(f.clone());
340341
}
341342

342343
let mut events =
@@ -423,7 +424,7 @@ impl SerialManager {
423424
previous_reader
424425
.shutdown(Shutdown::Both)
425426
.map_err(Error::AcceptConnection)?;
426-
write_distributor.remove_writer("tcp");
427+
write_distributor.remove_writer(TypeId::of::<TcpStream>());
427428
}
428429

429430
let ConsoleOutput::Tcp(ref listener, _) = in_file else {
@@ -449,7 +450,7 @@ impl SerialManager {
449450
),
450451
)
451452
.map_err(Error::Epoll)?;
452-
write_distributor.add_writer("tcp".into(), writer);
453+
write_distributor.add_writer(writer);
453454
}
454455
EpollDispatch::File => {
455456
if event.events & libc::EPOLLIN as u32 != 0 {
@@ -489,7 +490,7 @@ impl SerialManager {
489490
.shutdown(Shutdown::Both)
490491
.map_err(Error::ShutdownConnection)?;
491492
reader_tcp = None;
492-
write_distributor.remove_writer("tcp");
493+
write_distributor.remove_writer(TypeId::of::<TcpStream>());
493494
}
494495
count
495496
} else {

0 commit comments

Comments
 (0)