Skip to main content

zenoh_util/ffi/
mod.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14#[cfg(windows)]
15pub mod win;
16
17/// # Safety
18/// Dereferences raw pointer argument.
19/// ptr should be a valid string pointer.
20pub unsafe fn pwstr_to_string(ptr: *mut u16) -> String {
21    use std::slice::from_raw_parts;
22
23    // SAFETY: Dereference the raw pointer and call from_raw_parts.
24    let array: &[u16] = unsafe {
25        let len = (0_usize..)
26            .find(|&n| *ptr.add(n) == 0)
27            .expect("Couldn't find null terminator");
28        from_raw_parts(ptr, len)
29    };
30
31    String::from_utf16_lossy(array)
32}
33
34/// # Safety
35/// Dereferences raw pointer argument.
36/// ptr should be a valid string pointer.
37pub unsafe fn pstr_to_string(ptr: *mut i8) -> String {
38    use std::slice::from_raw_parts;
39
40    // SAFETY: Dereference the raw pointer and call from_raw_parts.
41    let array: &[u8] = unsafe {
42        let len = (0_usize..)
43            .find(|&n| *ptr.add(n) == 0)
44            .expect("Couldn't find null terminator");
45        from_raw_parts(ptr as *const u8, len)
46    };
47
48    String::from_utf8_lossy(array).to_string()
49}
50
51/// Struct used to safely exchange data in json format in plugins.
52/// It is not entirely abi stable due to using String, but should do
53/// for now since we require plugins to use the same version of rustc and
54/// same version of zenoh.
55#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
56#[serde(from = "serde_json::Value")]
57#[serde(into = "serde_json::Value")]
58pub struct JsonValue(String);
59
60impl PartialEq for JsonValue {
61    fn eq(&self, other: &Self) -> bool {
62        let left: serde_json::Value = self.into();
63        let right: serde_json::Value = other.into();
64        left == right
65    }
66}
67
68impl Eq for JsonValue {}
69
70impl From<&serde_json::Value> for JsonValue {
71    fn from(value: &serde_json::Value) -> Self {
72        JsonValue(serde_json::to_string(value).unwrap())
73    }
74}
75
76impl From<serde_json::Value> for JsonValue {
77    fn from(value: serde_json::Value) -> Self {
78        (&value).into()
79    }
80}
81
82impl From<&JsonValue> for serde_json::Value {
83    fn from(value: &JsonValue) -> Self {
84        serde_json::from_str(&value.0).unwrap()
85    }
86}
87
88impl From<JsonValue> for serde_json::Value {
89    fn from(value: JsonValue) -> Self {
90        (&value).into()
91    }
92}
93
94impl Default for JsonValue {
95    fn default() -> Self {
96        serde_json::Value::default().into()
97    }
98}
99
100impl JsonValue {
101    pub fn into_serde_value(&self) -> serde_json::Value {
102        self.into()
103    }
104}
105
106/// Struct used to safely exchange data in json format in plugins.
107/// It is not entirely abi stable due to using String, but should do
108/// for now since we require plugins to use the same version of rustc and
109/// same version of zenoh.
110#[derive(Debug, Clone)]
111pub struct JsonKeyValueMap(String);
112
113impl From<&serde_json::Map<String, serde_json::Value>> for JsonKeyValueMap {
114    fn from(value: &serde_json::Map<String, serde_json::Value>) -> Self {
115        JsonKeyValueMap(serde_json::to_string(value).unwrap())
116    }
117}
118
119impl From<serde_json::Map<String, serde_json::Value>> for JsonKeyValueMap {
120    fn from(value: serde_json::Map<String, serde_json::Value>) -> Self {
121        (&value).into()
122    }
123}
124
125impl From<&JsonKeyValueMap> for serde_json::Map<String, serde_json::Value> {
126    fn from(value: &JsonKeyValueMap) -> Self {
127        serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(&value.0).unwrap()
128    }
129}
130
131impl From<JsonKeyValueMap> for serde_json::Map<String, serde_json::Value> {
132    fn from(value: JsonKeyValueMap) -> Self {
133        (&value).into()
134    }
135}
136
137impl Default for JsonKeyValueMap {
138    fn default() -> Self {
139        serde_json::Map::<String, serde_json::Value>::default().into()
140    }
141}
142
143impl PartialEq for JsonKeyValueMap {
144    fn eq(&self, other: &Self) -> bool {
145        let left: serde_json::Map<String, serde_json::Value> = self.into();
146        let right: serde_json::Map<String, serde_json::Value> = other.into();
147        left == right
148    }
149}
150
151impl Eq for JsonKeyValueMap {}
152
153impl JsonKeyValueMap {
154    pub fn into_serde_map(&self) -> serde_json::Map<String, serde_json::Value> {
155        self.into()
156    }
157}