donet_core/
dcatomic.rs

1/*
2    This file is part of Donet.
3
4    Copyright © 2024 Max Rodriguez <[email protected]>
5
6    Donet is free software; you can redistribute it and/or modify
7    it under the terms of the GNU Affero General Public License,
8    as published by the Free Software Foundation, either version 3
9    of the License, or (at your option) any later version.
10
11    Donet is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU Affero General Public License for more details.
15
16    You should have received a copy of the GNU Affero General Public
17    License along with Donet. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Data model for a DC Atomic Field, which represents a remote
21//! procedure call method of a Distributed Class.
22
23use crate::dcfield::DCField;
24use crate::dckeyword::DCKeywordList;
25use crate::dcparameter::DCParameter;
26use crate::hashgen::*;
27
28/// Represents an atomic field of a Distributed Class.
29/// This defines the interface to a DClass object, and is
30/// always implemented as a remote procedure call (RPC).
31#[derive(Debug)]
32pub struct DCAtomicField<'dc> {
33    base_field: DCField<'dc>,
34    elements: Vec<&'dc DCParameter<'dc>>,
35}
36
37impl std::fmt::Display for DCAtomicField<'_> {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        writeln!(f, "TODO")
40    }
41}
42
43impl LegacyDCHash for DCAtomicField<'_> {
44    fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
45        self.base_field.generate_hash(hashgen);
46
47        hashgen.add_int(self.elements.len().try_into().unwrap());
48
49        for param in &self.elements {
50            param.generate_hash(hashgen);
51        }
52    }
53}
54
55impl<'dc> DCAtomicField<'dc> {
56    #[inline(always)]
57    pub fn get_num_elements(&self) -> usize {
58        self.elements.len()
59    }
60
61    #[inline(always)]
62    pub fn get_element(&self, index: usize) -> Option<&'dc DCParameter<'dc>> {
63        self.elements.get(index).copied()
64    }
65
66    pub fn set_keyword_list(&mut self, kw_list: DCKeywordList<'dc>) {
67        self.base_field.set_field_keyword_list(kw_list)
68    }
69}