embeddenator-interop 0.22.0

Kernel interop and system integration for Embeddenator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! FFI (Foreign Function Interface) bindings for C/C++ integration.
//!
//! This module provides C-compatible bindings for embeddenator core types.
//! All functions are marked `unsafe` and require careful memory management.
//!
//! ## Safety Considerations
//!
//! - All pointers must be valid and properly aligned
//! - String pointers must be null-terminated UTF-8
//! - Caller is responsible for freeing memory allocated by FFI functions
//! - No Rust objects should be accessed after being freed
//!
//! ## Example (C)
//!
//! ```c
//! #include "embeddenator_interop.h"
//!
//! // Create a vector
//! SparseVecHandle* vec = sparse_vec_new();
//!
//! // Use the vector...
//!
//! // Free the vector
//! sparse_vec_free(vec);
//! ```

use embeddenator_vsa::{ReversibleVSAConfig, SparseVec};
use std::ffi::CStr;
use std::os::raw::c_char;
use std::ptr;
use std::slice;

// ============================================================================
// Opaque Handle Types
// ============================================================================

/// Opaque handle to a SparseVec
#[repr(C)]
pub struct SparseVecHandle {
    _private: [u8; 0],
}

/// Opaque handle to a ReversibleVSAConfig
#[repr(C)]
pub struct VSAConfigHandle {
    _private: [u8; 0],
}

/// Result buffer for returning data to C
#[repr(C)]
pub struct ByteBuffer {
    pub data: *mut u8,
    pub len: usize,
    pub capacity: usize,
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Convert a Rust object to an opaque pointer
unsafe fn to_handle<T>(obj: T) -> *mut T {
    Box::into_raw(Box::new(obj))
}

/// Convert an opaque pointer back to a Rust object
unsafe fn from_handle<T>(handle: *mut T) -> Box<T> {
    assert!(!handle.is_null(), "FFI: null handle");
    Box::from_raw(handle)
}

/// Borrow an object from a handle without taking ownership
unsafe fn borrow_handle<T>(handle: *const T) -> &'static T {
    assert!(!handle.is_null(), "FFI: null handle");
    &*handle
}

/// Borrow an object mutably from a handle without taking ownership
#[allow(dead_code)]
unsafe fn borrow_handle_mut<T>(handle: *mut T) -> &'static mut T {
    assert!(!handle.is_null(), "FFI: null handle");
    &mut *handle
}

// ============================================================================
// SparseVec FFI
// ============================================================================

/// Create a new SparseVec
///
/// # Safety
/// Must call `sparse_vec_free` to deallocate
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_new() -> *mut SparseVecHandle {
    let vec = SparseVec::new();
    to_handle(vec) as *mut SparseVecHandle
}

/// Free a SparseVec
///
/// # Safety
/// - `handle` must be a valid pointer from `sparse_vec_new`
/// - Must not use `handle` after calling this function
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_free(handle: *mut SparseVecHandle) {
    if !handle.is_null() {
        let _ = from_handle(handle as *mut SparseVec);
    }
}

/// Bundle two SparseVecs
///
/// # Safety
/// - All handles must be valid
/// - Returns a new handle that must be freed with `sparse_vec_free`
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_bundle(
    a: *const SparseVecHandle,
    b: *const SparseVecHandle,
) -> *mut SparseVecHandle {
    let a_vec = borrow_handle(a as *const SparseVec);
    let b_vec = borrow_handle(b as *const SparseVec);
    let result = a_vec.bundle(b_vec);
    to_handle(result) as *mut SparseVecHandle
}

/// Bind two SparseVecs
///
/// # Safety
/// - All handles must be valid
/// - Returns a new handle that must be freed with `sparse_vec_free`
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_bind(
    a: *const SparseVecHandle,
    b: *const SparseVecHandle,
) -> *mut SparseVecHandle {
    let a_vec = borrow_handle(a as *const SparseVec);
    let b_vec = borrow_handle(b as *const SparseVec);
    let result = a_vec.bind(b_vec);
    to_handle(result) as *mut SparseVecHandle
}

/// Compute cosine similarity between two SparseVecs
///
/// # Safety
/// All handles must be valid
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_cosine(
    a: *const SparseVecHandle,
    b: *const SparseVecHandle,
) -> f64 {
    let a_vec = borrow_handle(a as *const SparseVec);
    let b_vec = borrow_handle(b as *const SparseVec);
    a_vec.cosine(b_vec)
}

/// Serialize a SparseVec to JSON
///
/// # Safety
/// - `handle` must be valid
/// - Returns a ByteBuffer that must be freed with `byte_buffer_free`
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_to_json(handle: *const SparseVecHandle) -> ByteBuffer {
    let vec = borrow_handle(handle as *const SparseVec);
    match serde_json::to_vec(vec) {
        Ok(mut bytes) => {
            let len = bytes.len();
            let capacity = bytes.capacity();
            let data = bytes.as_mut_ptr();
            std::mem::forget(bytes);
            ByteBuffer {
                data,
                len,
                capacity,
            }
        }
        Err(_) => ByteBuffer {
            data: ptr::null_mut(),
            len: 0,
            capacity: 0,
        },
    }
}

/// Deserialize a SparseVec from JSON
///
/// # Safety
/// - `data` must point to valid JSON bytes
/// - `len` must be the correct length
/// - Returns a handle that must be freed with `sparse_vec_free`
#[no_mangle]
pub unsafe extern "C" fn sparse_vec_from_json(data: *const u8, len: usize) -> *mut SparseVecHandle {
    if data.is_null() {
        return ptr::null_mut();
    }
    let bytes = slice::from_raw_parts(data, len);
    match serde_json::from_slice::<SparseVec>(bytes) {
        Ok(vec) => to_handle(vec) as *mut SparseVecHandle,
        Err(_) => ptr::null_mut(),
    }
}

// ============================================================================
// VSAConfig FFI
// ============================================================================

/// Create a new default ReversibleVSAConfig
///
/// # Safety
/// Must call `vsa_config_free` to deallocate
#[no_mangle]
pub unsafe extern "C" fn vsa_config_new() -> *mut VSAConfigHandle {
    let config = ReversibleVSAConfig::default();
    to_handle(config) as *mut VSAConfigHandle
}

/// Create a new ReversibleVSAConfig with custom parameters
///
/// # Safety
/// Must call `vsa_config_free` to deallocate
#[no_mangle]
pub unsafe extern "C" fn vsa_config_new_custom(
    block_size: usize,
    max_path_depth: usize,
    base_shift: usize,
    target_sparsity: usize,
) -> *mut VSAConfigHandle {
    let config = ReversibleVSAConfig {
        block_size,
        max_path_depth,
        base_shift,
        target_sparsity,
    };
    to_handle(config) as *mut VSAConfigHandle
}

/// Free a VSAConfig
///
/// # Safety
/// - `handle` must be a valid pointer from `vsa_config_new*`
/// - Must not use `handle` after calling this function
#[no_mangle]
pub unsafe extern "C" fn vsa_config_free(handle: *mut VSAConfigHandle) {
    if !handle.is_null() {
        let _ = from_handle(handle as *mut ReversibleVSAConfig);
    }
}

/// Encode data into a SparseVec
///
/// # Safety
/// - All handles must be valid
/// - `data` must point to valid bytes
/// - `len` must be correct
/// - `path` may be null or must be null-terminated UTF-8
/// - Returns a handle that must be freed with `sparse_vec_free`
#[no_mangle]
pub unsafe extern "C" fn vsa_encode_data(
    config: *const VSAConfigHandle,
    data: *const u8,
    len: usize,
    path: *const c_char,
) -> *mut SparseVecHandle {
    let config_ref = borrow_handle(config as *const ReversibleVSAConfig);
    let bytes = slice::from_raw_parts(data, len);

    let path_str = if path.is_null() {
        None
    } else {
        CStr::from_ptr(path).to_str().ok()
    };

    let vec = SparseVec::encode_data(bytes, config_ref, path_str);
    to_handle(vec) as *mut SparseVecHandle
}

/// Decode a SparseVec back to data
///
/// # Safety
/// - All handles must be valid
/// - `path` may be null or must be null-terminated UTF-8
/// - Returns a ByteBuffer that must be freed with `byte_buffer_free`
#[no_mangle]
pub unsafe extern "C" fn vsa_decode_data(
    config: *const VSAConfigHandle,
    vec: *const SparseVecHandle,
    path: *const c_char,
    expected_size: usize,
) -> ByteBuffer {
    let config_ref = borrow_handle(config as *const ReversibleVSAConfig);
    let vec_ref = borrow_handle(vec as *const SparseVec);

    let path_str = if path.is_null() {
        None
    } else {
        CStr::from_ptr(path).to_str().ok()
    };

    let mut decoded = vec_ref.decode_data(config_ref, path_str, expected_size);
    let len = decoded.len();
    let capacity = decoded.capacity();
    let data = decoded.as_mut_ptr();
    std::mem::forget(decoded);

    ByteBuffer {
        data,
        len,
        capacity,
    }
}

// ============================================================================
// ByteBuffer Management
// ============================================================================

/// Free a ByteBuffer returned by FFI functions
///
/// # Safety
/// - `buffer` must be a valid ByteBuffer returned by this library
/// - Must not use `buffer` after calling this function
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_free(buffer: ByteBuffer) {
    if !buffer.data.is_null() {
        let _ = Vec::from_raw_parts(buffer.data, buffer.len, buffer.capacity);
    }
}

// ============================================================================
// Error Handling
// ============================================================================

/// Get the last error message (if any)
///
/// # Safety
/// - Returns a pointer to a static string (do not free)
/// - Returns null if no error occurred
#[no_mangle]
pub unsafe extern "C" fn embeddenator_last_error() -> *const c_char {
    // Thread-local error storage could be added here
    ptr::null()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sparse_vec_create_free() {
        unsafe {
            let handle = sparse_vec_new();
            assert!(!handle.is_null());
            sparse_vec_free(handle);
        }
    }

    #[test]
    fn test_sparse_vec_operations() {
        unsafe {
            let a = sparse_vec_new();
            let b = sparse_vec_new();

            let bundled = sparse_vec_bundle(a, b);
            assert!(!bundled.is_null());

            let bound = sparse_vec_bind(a, b);
            assert!(!bound.is_null());

            let cosine = sparse_vec_cosine(a, b);
            assert!(cosine.is_finite());

            sparse_vec_free(bundled);
            sparse_vec_free(bound);
            sparse_vec_free(a);
            sparse_vec_free(b);
        }
    }

    #[test]
    fn test_sparse_vec_json_roundtrip() {
        unsafe {
            let vec = sparse_vec_new();

            let buffer = sparse_vec_to_json(vec);
            assert!(!buffer.data.is_null());
            assert!(buffer.len > 0);

            let decoded = sparse_vec_from_json(buffer.data, buffer.len);
            assert!(!decoded.is_null());

            byte_buffer_free(buffer);
            sparse_vec_free(vec);
            sparse_vec_free(decoded);
        }
    }

    #[test]
    fn test_vsa_config() {
        unsafe {
            let config = vsa_config_new();
            assert!(!config.is_null());
            vsa_config_free(config);

            let custom = vsa_config_new_custom(256, 10, 1000, 200);
            assert!(!custom.is_null());
            vsa_config_free(custom);
        }
    }

    #[test]
    fn test_encode_decode() {
        unsafe {
            let config = vsa_config_new();
            let data = b"Hello, FFI!";

            let vec = vsa_encode_data(config, data.as_ptr(), data.len(), ptr::null());
            assert!(!vec.is_null());

            let decoded = vsa_decode_data(config, vec, ptr::null(), data.len());
            assert!(!decoded.data.is_null());
            assert_eq!(decoded.len, data.len());

            byte_buffer_free(decoded);
            sparse_vec_free(vec);
            vsa_config_free(config);
        }
    }
}