#[no_mangle]
pub unsafe extern "C" fn vs_iter<'a>(
    vs: *mut VoluntaryServitude<*const c_void>
) -> *mut VSIter<'a, *const c_void>
Expand description

Makes lock-free iterator based on VoluntaryServitude

vs_iter_drop should be called eventually for VSIter returned, otherwise memory will leak

Returns NULL if pointer to VoluntaryServitude is NULL

Warning: UB if pointer to VoluntaryServitude is invalid

Rust

use std::ptr::null_mut;
use voluntary_servitude::ffi::*;

unsafe {
    let vs = vs_new();
    let data: i32 = 3;
    assert_eq!(vs_append(vs, &data as *const i32 as *const c_void), 0);
    let iter = vs_iter(vs);
    assert_eq!(vs_destroy(vs), 0);
    assert_eq!(*(vs_iter_next(iter) as *const i32), 3);
    assert!(vs_iter_next(iter).is_null());
    assert_eq!(vs_iter_destroy(iter), 0);

    // Propagates NULL pointers
    assert_eq!(vs_iter(null_mut()), null_mut());
}

C

#include<assert.h>
#include "../include/voluntary_servitude.h"

int main(int argc, char **argv) {
    vs_t * vs = vs_new();
    vs_iter_t * iter = vs_iter(vs);
    const unsigned int data = 3;
    assert(vs_append(vs, (void *) &data) == 0);
    vs_iter_t * iter2 = vs_iter(vs);
    assert(vs_destroy(iter) == 0);
    assert(*(unsigned int *) vs_iter_next(iter2) == 3);
    assert(vs_iter_next(iter2) == NULL);

    assert(vs_iter_destroy(iter2) == 0);

    // Propagates NULL pointers
    assert(vs_iter(NULL) == NULL);
    return 0;
}