devicetree_tool/
reservation.rs

1// Copyright (c) 2023, Michael Zhao
2// SPDX-License-Identifier: MIT
3
4use crate::dts_generator::DtsGenerator;
5
6/// A memory reservation block for reserving physical memory.
7#[derive(Copy, Clone)]
8pub struct Reservation {
9    pub address: u64,
10    pub length: u64,
11}
12
13/// Create a new memory reservation block with physical address and length
14///
15/// Example:
16///
17/// ```
18/// use devicetree_tool::Reservation;
19///
20/// let resv = Reservation::new(0, 0x1000);
21///
22/// assert_eq!(format!("{}", resv), "/memreserve/ 0x0000000000000000 0x0000000000001000;");
23/// ```
24impl Reservation {
25    pub fn new(address: u64, length: u64) -> Self {
26        Reservation { address, length }
27    }
28}
29
30impl std::fmt::Display for Reservation {
31    /// Print a `Property` in the format of DTS
32    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
33        let s = DtsGenerator::generate_reservation(self, 0);
34        write!(f, "{s}")
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_reservation_print() {
44        let reservation = Reservation::new(0x0, 0x100000);
45        let printing = format!("{}", reservation);
46        assert_eq!(
47            &printing,
48            "/memreserve/ 0x0000000000000000 0x0000000000100000;"
49        );
50    }
51}