1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::Consumption;
use std::mem::{transmute, transmute_copy};
pub type Prices = [i32; 24*7];
#[derive(Debug)]
pub struct IntegerConsumption {
pub hour_of_week: u64,
pub units_consumed: i32,
}
impl Consumption<i32, u64> for IntegerConsumption {
type Prices = Prices;
fn is_valid(&self) -> bool {
if self.units_consumed < 0 {
println!("invalid consumption: cons = {}", self.units_consumed);
return false;
}
true
}
fn new(cons: i32, other: u64) -> IntegerConsumption {
let ret = IntegerConsumption {
hour_of_week: other,
units_consumed: cons,
};
assert!(ret.is_valid());
ret
}
fn null_prices() -> Prices { [0; 24*7] }
fn set_price(prices: &mut Prices, other: u64, price: i32) { prices[(other % (24*7)) as usize] = price }
fn get_price(prices: &Prices, other: u64) -> i32 { prices[(other % (24*7)) as usize] }
fn prices_len() -> usize {24*7}
fn cons_from_bytes(bytes: &[u8]) -> i32 {
assert!(bytes.len() == 4);
let mut fixed_size = [0 as u8; 4];
for i in 0..4 {
fixed_size[i] = bytes[i];
}
unsafe { transmute::<[u8; 4], i32>(fixed_size) }
}
fn prices_from_bytes(bytes: &[u8]) -> Prices {
assert!(bytes.len() == 24*7*4);
let mut fixed_size = [0 as u8; 24*7*4];
for i in 0..(24*7*4) {
fixed_size[i] = bytes[i];
}
unsafe { transmute::<[u8; 24*7*4], Prices>(fixed_size) }
}
fn prices_to_bytes(prices: &Prices) -> Vec<u8> {
let array = unsafe { transmute_copy::<Prices, [u8; 24*7*4]>(prices) };
Vec::<u8>::from(array.as_ref())
}
}