Use entry api for `DeviceAddedEvent`s seat creation/update

This commit is contained in:
Drakulix 2017-04-15 19:19:09 +02:00
parent 10857d108e
commit ab35f140a0
1 changed files with 21 additions and 19 deletions

View File

@ -6,7 +6,7 @@ use input::{Libinput, Device, Seat as LibinputSeat, DeviceCapability};
use input::event::*;
use std::io::Error as IoError;
use std::collections::hash_map::{DefaultHasher, HashMap};
use std::collections::hash_map::{DefaultHasher, Entry, HashMap};
use std::hash::{Hash, Hasher};
pub struct LibinputInputBackend {
@ -73,9 +73,9 @@ impl InputBackend for LibinputInputBackend {
let device_seat = added.seat();
self.devices.push(added);
let contains = self.seats.contains_key(&device_seat);
if contains {
let old_seat = self.seats.get_mut(&device_seat).unwrap();
match self.seats.entry(device_seat) {
Entry::Occupied(mut seat_entry) => {
let old_seat = seat_entry.get_mut();
{
let caps = old_seat.capabilities_mut();
caps.pointer = new_caps.pointer || caps.pointer;
@ -85,12 +85,14 @@ impl InputBackend for LibinputInputBackend {
if let Some(ref mut handler) = self.handler {
handler.on_seat_changed(old_seat);
}
} else {
},
Entry::Vacant(seat_entry) => {
let mut hasher = DefaultHasher::default();
device_seat.hash(&mut hasher);
self.seats.insert(device_seat.clone(), Seat::new(hasher.finish(), new_caps));
seat_entry.key().hash(&mut hasher);
let seat = seat_entry.insert(Seat::new(hasher.finish(), new_caps));
if let Some(ref mut handler) = self.handler {
handler.on_seat_created(self.seats.get(&device_seat).unwrap());
handler.on_seat_created(seat);
}
}
}
},