-
Notifications
You must be signed in to change notification settings - Fork 76
/
storage.rs
141 lines (112 loc) · 3.42 KB
/
storage.rs
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
use std::os::raw::c_char;
use std::slice;
use std::sync::Arc;
use crate::ffi::utils::{ForeignObject, SourmashStr};
use crate::prelude::*;
use crate::storage::ZipStorage;
pub struct SourmashZipStorage;
impl ForeignObject for SourmashZipStorage {
type RustObject = Arc<ZipStorage>;
}
ffi_fn! {
unsafe fn zipstorage_new(ptr: *const c_char, insize: usize) -> Result<*mut SourmashZipStorage> {
let path = {
assert!(!ptr.is_null());
let path = slice::from_raw_parts(ptr as *mut u8, insize);
std::str::from_utf8(path)?
};
let zipstorage = ZipStorage::from_file(path)?;
Ok(SourmashZipStorage::from_rust(Arc::new(zipstorage)))
}
}
#[no_mangle]
pub unsafe extern "C" fn zipstorage_free(ptr: *mut SourmashZipStorage) {
SourmashZipStorage::drop(ptr);
}
ffi_fn! {
unsafe fn zipstorage_load(ptr: *const SourmashZipStorage,
path_ptr: *const c_char,
insize: usize,
size: *mut usize) -> Result<*const u8> {
let storage = SourmashZipStorage::as_rust(ptr);
let path = {
assert!(!path_ptr.is_null());
let path = slice::from_raw_parts(path_ptr as *mut u8, insize);
std::str::from_utf8(path)?
};
let buffer = storage.load(path)?;
let b = buffer.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *const u8)
}
}
ffi_fn! {
unsafe fn zipstorage_list_sbts(
ptr: *const SourmashZipStorage,
size: *mut usize,
) -> Result<*mut *mut SourmashStr> {
let storage = SourmashZipStorage::as_rust(ptr);
let sbts = storage.list_sbts()?;
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*mut SourmashStr> = sbts
.into_iter()
.map(|x| Box::into_raw(Box::new(SourmashStr::from_string(x))))
.collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *mut *mut SourmashStr)
}
}
ffi_fn! {
unsafe fn zipstorage_filenames(
ptr: *const SourmashZipStorage,
size: *mut usize,
) -> Result<*mut *mut SourmashStr> {
let storage = SourmashZipStorage::as_rust(ptr);
let files = storage.filenames()?;
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*mut SourmashStr> = files
.into_iter()
.map(|x| Box::into_raw(Box::new(SourmashStr::from_string(x))))
.collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *mut *mut SourmashStr)
}
}
ffi_fn! {
unsafe fn zipstorage_set_subdir(
ptr: *mut SourmashZipStorage,
path_ptr: *const c_char,
insize: usize,
) -> Result<()> {
let storage = SourmashZipStorage::as_rust_mut(ptr);
let path = {
assert!(!path_ptr.is_null());
let path = slice::from_raw_parts(path_ptr as *mut u8, insize);
std::str::from_utf8(path)?
};
(*Arc::get_mut(storage).unwrap()).set_subdir(path.to_string());
Ok(())
}
}
ffi_fn! {
unsafe fn zipstorage_path(ptr: *const SourmashZipStorage) -> Result<SourmashStr> {
let storage = SourmashZipStorage::as_rust(ptr);
if let Some(ref path) = storage.path() {
Ok(path.clone().into_string().into())
} else {
Ok("".into())
}
}
}
ffi_fn! {
unsafe fn zipstorage_subdir(ptr: *const SourmashZipStorage) -> Result<SourmashStr> {
let storage = SourmashZipStorage::as_rust(ptr);
if let Some(ref path) = storage.subdir() {
Ok(path.clone().into())
} else {
Ok("".into())
}
}
}