starnix_rcu/
rcu_string.rs1use fuchsia_rcu::{RcuCell, RcuReadScope};
6use starnix_types::string::{FsStr, FsString};
7
8#[derive(Debug, Default)]
13pub struct RcuString {
14 cell: RcuCell<FsString>,
15}
16
17impl RcuString {
18 pub fn new(value: impl Into<FsString>) -> Self {
20 Self { cell: RcuCell::new(value.into()) }
21 }
22
23 pub fn read<'a>(&self, scope: &'a RcuReadScope) -> &'a FsStr {
27 self.cell.as_ref(scope).as_ref()
28 }
29
30 pub fn update(&self, value: impl Into<FsString>) {
35 self.cell.update(value.into());
36 }
37}
38
39impl From<FsString> for RcuString {
40 fn from(value: FsString) -> Self {
41 Self::new(value)
42 }
43}
44
45impl From<&FsStr> for RcuString {
46 fn from(value: &FsStr) -> Self {
47 Self::new(value)
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_rcu_string_read() {
57 let s = RcuString::new("hello");
58 let scope = RcuReadScope::new();
59 assert_eq!(s.read(&scope), "hello");
60 }
61
62 #[test]
63 fn test_rcu_string_update() {
64 let s = RcuString::new("initial");
65
66 {
68 let scope = RcuReadScope::new();
69 assert_eq!(s.read(&scope), "initial");
70 }
71
72 s.update("updated");
74
75 {
77 let scope = RcuReadScope::new();
78 assert_eq!(s.read(&scope), "updated");
79 }
80 }
81}