Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

current.rs 935B

123456789101112131415161718192021222324252627282930313233343536373839
  1. use std::collections::HashMap;
  2. use log::error;
  3. use protocol::{Location, Value, ValueType};
  4. pub struct CurrentValues {
  5. map: HashMap<Key, i64>,
  6. }
  7. impl CurrentValues {
  8. pub fn new() -> CurrentValues {
  9. CurrentValues {
  10. map: HashMap::new(),
  11. }
  12. }
  13. pub fn set(&mut self, location: Location, value: Value) {
  14. let (type_, value) = match value.split() {
  15. Some(x) => x,
  16. None => {
  17. error!("Invalid value passed to CurrentValues::set().");
  18. return;
  19. }
  20. };
  21. self.map.insert(Key { location, type_ }, value);
  22. }
  23. pub fn get(&self, location: Location, type_: ValueType) -> Option<Value> {
  24. let entry = self.map.get(&Key { location, type_ })?.clone();
  25. Some(Value::combine(type_, entry))
  26. }
  27. }
  28. #[derive(Copy, Clone, PartialEq, Eq, Hash)]
  29. struct Key {
  30. location: Location,
  31. type_: ValueType,
  32. }