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
19pub unsafe fn pwstr_to_string(ptr: *mut u16) -> String {
20    use std::slice::from_raw_parts;
21    let len = (0_usize..)
22        .find(|&n| *ptr.add(n) == 0)
23        .expect("Couldn't find null terminator");
24    let array: &[u16] = from_raw_parts(ptr, len);
25    String::from_utf16_lossy(array)
26}
27
28/// # Safety
29/// Dereferences raw pointer argument
30pub unsafe fn pstr_to_string(ptr: *mut i8) -> String {
31    use std::slice::from_raw_parts;
32    let len = (0_usize..)
33        .find(|&n| *ptr.add(n) == 0)
34        .expect("Couldn't find null terminator");
35    let array: &[u8] = from_raw_parts(ptr as *const u8, len);
36    String::from_utf8_lossy(array).to_string()
37}
38
39/// Struct used to safely exchange data in json format in plugins.
40/// It is not entirely abi stable due to using String, but should do
41/// for now since we require plugins to use the same version of rustc and
42/// same version of zenoh.
43#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
44#[serde(from = "serde_json::Value")]
45#[serde(into = "serde_json::Value")]
46pub struct JsonValue(String);
47
48impl PartialEq for JsonValue {
49    fn eq(&self, other: &Self) -> bool {
50        let left: serde_json::Value = self.into();
51        let right: serde_json::Value = other.into();
52        left == right
53    }
54}
55
56impl Eq for JsonValue {}
57
58impl From<&serde_json::Value> for JsonValue {
59    fn from(value: &serde_json::Value) -> Self {
60        JsonValue(serde_json::to_string(value).unwrap())
61    }
62}
63
64impl From<serde_json::Value> for JsonValue {
65    fn from(value: serde_json::Value) -> Self {
66        (&value).into()
67    }
68}
69
70impl From<&JsonValue> for serde_json::Value {
71    fn from(value: &JsonValue) -> Self {
72        serde_json::from_str(&value.0).unwrap()
73    }
74}
75
76impl From<JsonValue> for serde_json::Value {
77    fn from(value: JsonValue) -> Self {
78        (&value).into()
79    }
80}
81
82impl Default for JsonValue {
83    fn default() -> Self {
84        serde_json::Value::default().into()
85    }
86}
87
88impl JsonValue {
89    pub fn into_serde_value(&self) -> serde_json::Value {
90        self.into()
91    }
92}
93
94/// Struct used to safely exchange data in json format in plugins.
95/// It is not entirely abi stable due to using String, but should do
96/// for now since we require plugins to use the same version of rustc and
97/// same version of zenoh.
98#[derive(Debug, Clone)]
99pub struct JsonKeyValueMap(String);
100
101impl From<&serde_json::Map<String, serde_json::Value>> for JsonKeyValueMap {
102    fn from(value: &serde_json::Map<String, serde_json::Value>) -> Self {
103        JsonKeyValueMap(serde_json::to_string(value).unwrap())
104    }
105}
106
107impl From<serde_json::Map<String, serde_json::Value>> for JsonKeyValueMap {
108    fn from(value: serde_json::Map<String, serde_json::Value>) -> Self {
109        (&value).into()
110    }
111}
112
113impl From<&JsonKeyValueMap> for serde_json::Map<String, serde_json::Value> {
114    fn from(value: &JsonKeyValueMap) -> Self {
115        serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(&value.0).unwrap()
116    }
117}
118
119impl From<JsonKeyValueMap> for serde_json::Map<String, serde_json::Value> {
120    fn from(value: JsonKeyValueMap) -> Self {
121        (&value).into()
122    }
123}
124
125impl Default for JsonKeyValueMap {
126    fn default() -> Self {
127        serde_json::Map::<String, serde_json::Value>::default().into()
128    }
129}
130
131impl PartialEq for JsonKeyValueMap {
132    fn eq(&self, other: &Self) -> bool {
133        let left: serde_json::Map<String, serde_json::Value> = self.into();
134        let right: serde_json::Map<String, serde_json::Value> = other.into();
135        left == right
136    }
137}
138
139impl Eq for JsonKeyValueMap {}
140
141impl JsonKeyValueMap {
142    pub fn into_serde_map(&self) -> serde_json::Map<String, serde_json::Value> {
143        self.into()
144    }
145}