falco_plugin/tables/export/
table_box.rs1use crate::tables::export::entry::extensible::ExtensibleEntry;
2use crate::tables::export::table::{TableData, TableEntryType};
3use crate::tables::export::traits::{Entry, TableMetadata};
4use crate::tables::export::{FieldRef, HasMetadata, RefShared};
5use crate::tables::{FieldTypeId, Key};
6use falco_plugin_api::{ss_plugin_state_data, ss_plugin_table_fieldinfo, ss_plugin_table_input};
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9use std::ffi::CStr;
10use std::fmt::{Debug, Formatter};
11use std::ops::{Deref, DerefMut};
12use std::ptr::NonNull;
13
14pub struct Table<K, E>
36where
37 K: Key + Ord,
38 K: Borrow<<K as Key>::Borrowed>,
39 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
40 E: Entry,
41 E::Metadata: TableMetadata,
42{
43 ptr: NonNull<TableData<K, E>>,
44}
45
46impl<K, E> Table<K, E>
47where
48 K: Key + Ord,
49 K: Borrow<<K as Key>::Borrowed>,
50 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
51 E: Entry,
52 E::Metadata: TableMetadata,
53{
54 pub(crate) fn wrap(value: TableData<K, E>) -> Self {
56 let ptr = Box::into_raw(Box::new(value));
57 Self {
59 ptr: unsafe { NonNull::new_unchecked(ptr) },
60 }
61 }
62
63 pub(crate) fn as_mut_ptr(this: &Self) -> *mut TableData<K, E> {
65 this.ptr.as_ptr()
66 }
67
68 pub(crate) fn get_vtable(&self) -> *mut ss_plugin_table_input {
70 let table_ptr = Self::as_mut_ptr(self);
71 (**self).get_vtable_with_ptr(table_ptr)
72 }
73
74 pub fn new(name: &'static CStr) -> Result<Self, anyhow::Error> {
76 Ok(Self::wrap(TableData::new(name)?))
77 }
78
79 pub fn new_with_metadata(
83 tag: &'static CStr,
84 metadata: &<Self as HasMetadata>::Metadata,
85 ) -> Result<Self, anyhow::Error> {
86 Ok(Self::wrap(TableData::new_with_metadata(tag, metadata)?))
87 }
88
89 pub fn data(&self) -> RefShared<BTreeMap<K, RefShared<ExtensibleEntry<E>>>> {
99 (**self).data()
100 }
101
102 pub fn name(&self) -> &'static CStr {
104 (**self).name()
105 }
106
107 pub fn size(&self) -> usize {
109 (**self).size()
110 }
111
112 pub fn lookup<Q>(&self, key: &Q) -> Option<TableEntryType<E>>
114 where
115 K: Borrow<Q>,
116 Q: Ord + ?Sized,
117 {
118 (**self).lookup(key)
119 }
120
121 pub fn get_field_value(
123 &self,
124 entry: &TableEntryType<E>,
125 field: &crate::tables::export::field_descriptor::FieldDescriptor,
126 out: &mut ss_plugin_state_data,
127 ) -> Result<(), anyhow::Error> {
128 (**self).get_field_value(entry, field, out)
129 }
130
131 pub fn iterate_entries<F>(&self, func: F) -> bool
136 where
137 F: FnMut(&mut TableEntryType<E>) -> bool,
138 {
139 (**self).iterate_entries(func)
140 }
141
142 pub fn clear(&mut self) {
144 (**self).clear()
145 }
146
147 pub fn erase<Q>(&mut self, key: &Q) -> Option<TableEntryType<E>>
149 where
150 K: Borrow<Q>,
151 Q: Ord + ?Sized,
152 {
153 (**self).erase(key)
154 }
155
156 pub fn create_entry(&self) -> Result<TableEntryType<E>, anyhow::Error> {
160 (**self).create_entry()
161 }
162
163 pub fn create_entry_fn(
180 &self,
181 ) -> impl Fn() -> Result<RefShared<ExtensibleEntry<E>>, anyhow::Error> + use<K, E> {
182 (**self).create_entry_fn()
183 }
184
185 pub fn insert<Q>(&mut self, key: &Q, entry: TableEntryType<E>) -> Option<TableEntryType<E>>
187 where
188 K: Borrow<Q>,
189 Q: Ord + ToOwned<Owned = K> + ?Sized,
190 {
191 (**self).insert(key, entry)
192 }
193
194 pub fn write(
196 &self,
197 entry: &mut TableEntryType<E>,
198 field: &crate::tables::export::field_descriptor::FieldDescriptor,
199 value: &ss_plugin_state_data,
200 ) -> Result<(), anyhow::Error> {
201 (**self).write(entry, field, value)
202 }
203
204 pub fn list_fields(&self) -> &[ss_plugin_table_fieldinfo] {
206 (**self).list_fields()
207 }
208
209 pub fn get_field(&self, name: &CStr, field_type: FieldTypeId) -> Option<FieldRef> {
213 (**self).get_field(name, field_type)
214 }
215
216 pub fn add_field(
218 &self,
219 name: &CStr,
220 field_type: FieldTypeId,
221 read_only: bool,
222 ) -> Option<FieldRef> {
223 (**self).add_field(name, field_type, read_only)
224 }
225}
226
227impl<K, E> Deref for Table<K, E>
228where
229 K: Key + Ord,
230 K: Borrow<<K as Key>::Borrowed>,
231 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
232 E: Entry,
233 E::Metadata: TableMetadata,
234{
235 type Target = TableData<K, E>;
236 fn deref(&self) -> &TableData<K, E> {
237 unsafe { self.ptr.as_ref() }
239 }
240}
241
242impl<K, E> DerefMut for Table<K, E>
243where
244 K: Key + Ord,
245 K: Borrow<<K as Key>::Borrowed>,
246 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
247 E: Entry,
248 E::Metadata: TableMetadata,
249{
250 fn deref_mut(&mut self) -> &mut TableData<K, E> {
251 unsafe { self.ptr.as_mut() }
253 }
254}
255
256impl<K, E> Drop for Table<K, E>
257where
258 K: Key + Ord,
259 K: Borrow<<K as Key>::Borrowed>,
260 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
261 E: Entry,
262 E::Metadata: TableMetadata,
263{
264 fn drop(&mut self) {
265 unsafe {
267 drop(Box::from_raw(self.ptr.as_ptr()));
268 }
269 }
270}
271
272impl<K, E> Debug for Table<K, E>
273where
274 K: Key + Ord + Debug,
275 K: Borrow<<K as Key>::Borrowed>,
276 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
277 E: Entry + Debug,
278 E::Metadata: TableMetadata + Debug,
279{
280 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
281 (**self).fmt(f)
282 }
283}
284
285unsafe impl<K, E> Send for Table<K, E>
287where
288 K: Key + Ord + Send,
289 K: Borrow<<K as Key>::Borrowed>,
290 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
291 E: Entry + Send,
292 E::Metadata: TableMetadata,
293{
294}
295unsafe impl<K, E> Sync for Table<K, E>
296where
297 K: Key + Ord + Sync,
298 K: Borrow<<K as Key>::Borrowed>,
299 <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
300 E: Entry + Sync,
301 E::Metadata: TableMetadata,
302{
303}