micromegas_transit/
static_string.rs

1use crate::{
2    read_consume_pod, string_codec::StringCodec, write_any, InProcSerialize, InProcSize, Reflect,
3    UserDefinedType,
4};
5use lazy_static::lazy_static;
6use std::sync::Arc;
7
8/// Utf8StaticStringDependency serializes the value of the pointer and the contents of the string
9/// It should not be part of the event - it's the dependency of the StringId
10#[derive(Debug)]
11pub struct Utf8StaticStringDependency {
12    pub len: u32,
13    pub ptr: *const u8,
14}
15
16impl std::convert::From<&str> for Utf8StaticStringDependency {
17    fn from(src: &str) -> Self {
18        Self {
19            len: src.len() as u32,
20            ptr: src.as_ptr(),
21        }
22    }
23}
24
25// dummy impl for Reflect
26impl Reflect for Utf8StaticStringDependency {
27    fn reflect() -> UserDefinedType {
28        lazy_static! {
29            static ref TYPE_NAME: Arc<String> = Arc::new("StaticString".into());
30        }
31        UserDefinedType {
32            name: TYPE_NAME.clone(),
33            size: 0,
34            members: vec![],
35            is_reference: false,
36            secondary_udts: vec![],
37        }
38    }
39}
40
41impl InProcSerialize for Utf8StaticStringDependency {
42    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
43
44    fn get_value_size(&self) -> Option<u32> {
45        let id_size = std::mem::size_of::<usize>() as u32;
46        Some(self.len + id_size)
47    }
48
49    #[allow(unsafe_code)]
50    fn write_value(&self, buffer: &mut Vec<u8>) {
51        write_any(buffer, &self.ptr);
52        unsafe {
53            let slice = std::slice::from_raw_parts(self.ptr, self.len as usize);
54            buffer.extend_from_slice(slice);
55        }
56    }
57
58    #[allow(unsafe_code)]
59    unsafe fn read_value(mut window: &[u8]) -> Self {
60        let static_buffer_ptr: *const u8 = read_consume_pod(&mut window);
61        let buffer_size = window.len() as u32;
62        Self {
63            len: buffer_size,
64            ptr: static_buffer_ptr,
65        }
66    }
67}
68
69/// StaticStringDependency serializes the value of the pointer and the contents of the string
70/// It is designed to be wire-compatible with the unreal instrumentation
71#[derive(Debug)]
72pub struct StaticStringDependency {
73    pub codec: StringCodec,
74    pub len: u32,
75    pub ptr: *const u8,
76}
77
78// dummy impl for Reflect
79impl Reflect for StaticStringDependency {
80    fn reflect() -> UserDefinedType {
81        lazy_static! {
82            static ref TYPE_NAME: Arc<String> = Arc::new("StaticStringDependency".into());
83        }
84        UserDefinedType {
85            name: TYPE_NAME.clone(),
86            size: 0,
87            members: vec![],
88            is_reference: false,
89            secondary_udts: vec![],
90        }
91    }
92}
93
94impl InProcSerialize for StaticStringDependency {
95    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
96
97    fn get_value_size(&self) -> Option<u32> {
98        let id_size = std::mem::size_of::<usize>() as u32;
99        let size =
100			id_size +
101			1 + // codec
102			std::mem::size_of::<u32>() as u32 +// size in bytes
103			self.len // actual buffer
104			;
105        Some(size)
106    }
107
108    #[allow(unsafe_code)]
109    fn write_value(&self, buffer: &mut Vec<u8>) {
110        let id = self.ptr as u64;
111        write_any(buffer, &id);
112        let codec = self.codec as u8;
113        write_any(buffer, &codec);
114        write_any(buffer, &self.len);
115        unsafe {
116            let slice = std::slice::from_raw_parts(self.ptr, self.len as usize);
117            buffer.extend_from_slice(slice);
118        }
119    }
120
121    #[allow(unsafe_code)]
122    unsafe fn read_value(_window: &[u8]) -> Self {
123        // dependencies don't need to be read in the instrumented process
124        unimplemented!();
125    }
126}