fuchsia_inspect/writer/types/
string_reference.rsuse std::borrow::{Borrow, Cow};
use std::ops::Deref;
use std::sync::Arc;
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
pub struct StringReference(Arc<str>);
impl Deref for StringReference {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl Borrow<str> for StringReference {
fn borrow(&self) -> &str {
self.as_ref()
}
}
impl AsRef<str> for StringReference {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl From<&'_ StringReference> for StringReference {
fn from(sref: &'_ StringReference) -> Self {
sref.clone()
}
}
impl From<String> for StringReference {
fn from(data: String) -> Self {
StringReference(Arc::from(data.into_boxed_str()))
}
}
impl From<Arc<str>> for StringReference {
fn from(data: Arc<str>) -> Self {
StringReference(data)
}
}
impl From<&String> for StringReference {
fn from(data: &String) -> Self {
StringReference(Arc::from(data.as_ref()))
}
}
impl From<&str> for StringReference {
fn from(data: &str) -> Self {
StringReference(data.into())
}
}
impl From<Cow<'_, str>> for StringReference {
fn from(data: Cow<'_, str>) -> Self {
StringReference(data.as_ref().into())
}
}
#[cfg(test)]
mod tests {
use crate::writer::testing_utils::{get_state, GetBlockExt};
use crate::writer::{Inspector, Node};
use diagnostics_assertions::assert_data_tree;
#[fuchsia::test]
fn string_references_as_names() {
let inspector = Inspector::default();
inspector.root().record_int("foo", 0);
let child = inspector.root().create_child("bar");
child.record_double("foo", 3.25);
assert_data_tree!(inspector, root: {
foo: 0i64,
bar: {
foo: 3.25,
},
});
{
let _baz_property = child.create_uint("baz", 4);
assert_data_tree!(inspector, root: {
foo: 0i64,
bar: {
baz: 4u64,
foo: 3.25,
},
});
}
assert_data_tree!(inspector, root: {
foo: 0i64,
bar: {
foo: 3.25,
},
});
let pre_loop_allocated =
inspector.state().unwrap().try_lock().unwrap().stats().allocated_blocks;
let pre_loop_deallocated =
inspector.state().unwrap().try_lock().unwrap().stats().deallocated_blocks;
for i in 0..300 {
child.record_int("bar", i);
}
assert_eq!(
inspector.state().unwrap().try_lock().unwrap().stats().allocated_blocks,
pre_loop_allocated + 300
);
assert_eq!(
inspector.state().unwrap().try_lock().unwrap().stats().deallocated_blocks,
pre_loop_deallocated
);
let pre_loop_count = pre_loop_allocated + 300;
for i in 0..300 {
child.record_int("abcd", i);
}
assert_eq!(
inspector.state().unwrap().try_lock().unwrap().stats().allocated_blocks,
pre_loop_count + 300 + 1 );
assert_eq!(
inspector.state().unwrap().try_lock().unwrap().stats().deallocated_blocks,
pre_loop_deallocated
);
}
#[fuchsia::test]
fn owned_method_argument_properties() {
let state = get_state(4096);
let root = Node::new_root(state);
let node = root.create_child("node");
{
let _string_property =
node.create_string(String::from("string_property"), String::from("test"));
let _bytes_property =
node.create_bytes(String::from("bytes_property"), vec![0, 1, 2, 3]);
let _double_property = node.create_double(String::from("double_property"), 1.0);
let _int_property = node.create_int(String::from("int_property"), 1);
let _uint_property = node.create_uint(String::from("uint_property"), 1);
node.get_block(|node_block| {
assert_eq!(node_block.child_count().unwrap(), 5);
});
}
node.get_block(|node_block| {
assert_eq!(node_block.child_count().unwrap(), 0);
});
}
}