Address review comments @rnewman

This commit is contained in:
Emily Toop 2018-04-06 10:02:42 +01:00
parent 65e31ed09e
commit 48cd1aa3ed
3 changed files with 60 additions and 84 deletions

View file

@ -57,6 +57,9 @@ pub use utils::strings::{
string_to_c_char, string_to_c_char,
}; };
pub type TypedValueIterator = vec::IntoIter<TypedValue>;
pub type TypedValueListIterator = vec::IntoIter<Vec<TypedValue>>;
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ExternTxReport { pub struct ExternTxReport {
@ -319,80 +322,80 @@ pub unsafe extern "C" fn row_at_index(rows: *mut Vec<Vec<TypedValue>>, index: c_
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rows_iter(rows: *mut Vec<Vec<TypedValue>>) -> *mut vec::IntoIter<Vec<TypedValue>> { pub unsafe extern "C" fn rows_iter(rows: *mut Vec<Vec<TypedValue>>) -> *mut TypedValueListIterator {
let result = Box::from_raw(rows); let result = Box::from_raw(rows);
Box::into_raw(Box::new(result.into_iter())) Box::into_raw(Box::new(result.into_iter()))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rows_iter_next(iter: *mut ::std::vec::IntoIter<Vec<TypedValue>>) -> *mut Vec<TypedValue> { pub unsafe extern "C" fn rows_iter_next(iter: *mut TypedValueListIterator) -> *mut Vec<TypedValue> {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| Box::into_raw(Box::new(v))) iter.next().map_or(std::ptr::null_mut(), |v| Box::into_raw(Box::new(v)))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter(values: *mut Vec<TypedValue>) -> *mut vec::IntoIter<TypedValue> { pub unsafe extern "C" fn values_iter(values: *mut Vec<TypedValue>) -> *mut TypedValueIterator {
let result = Box::from_raw(values); let result = Box::from_raw(values);
Box::into_raw(Box::new(result.into_iter())) Box::into_raw(Box::new(result.into_iter()))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next(iter: *mut vec::IntoIter<TypedValue>) -> *const TypedValue { pub unsafe extern "C" fn values_iter_next(iter: *mut TypedValueIterator) -> *const TypedValue {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| &v as *const TypedValue) iter.next().map_or(std::ptr::null_mut(), |v| &v as *const TypedValue)
} }
//as_long //as_long
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_long(iter: *mut vec::IntoIter<TypedValue>) -> *const i64 { pub unsafe extern "C" fn values_iter_next_as_long(iter: *mut TypedValueIterator) -> *const i64 {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| &v.into_long().expect("Typed value cannot be coerced into a Long") as *const i64) iter.next().map_or(std::ptr::null_mut(), |v| &v.into_long().expect("Typed value cannot be coerced into a Long") as *const i64)
} }
// as ref // as ref
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_entid(iter: *mut vec::IntoIter<TypedValue>) -> *const Entid { pub unsafe extern "C" fn values_iter_next_as_entid(iter: *mut TypedValueIterator) -> *const Entid {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| &v.into_entid().expect("Typed value cannot be coerced into am Entid") as *const Entid) iter.next().map_or(std::ptr::null_mut(), |v| &v.into_entid().expect("Typed value cannot be coerced into am Entid") as *const Entid)
} }
// as kw // as kw
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_kw(iter: *mut vec::IntoIter<TypedValue>) -> *const c_char { pub unsafe extern "C" fn values_iter_next_as_kw(iter: *mut TypedValueIterator) -> *const c_char {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| string_to_c_char(v.into_kw().expect("Typed value cannot be coerced into a Namespaced Keyword").to_string())) iter.next().map_or(std::ptr::null_mut(), |v| string_to_c_char(v.into_kw().expect("Typed value cannot be coerced into a Namespaced Keyword").to_string()))
} }
//as_boolean //as_boolean
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_boolean(iter: *mut vec::IntoIter<TypedValue>) -> *const bool { pub unsafe extern "C" fn values_iter_next_as_boolean(iter: *mut TypedValueIterator) -> *const bool {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| &v.into_boolean().expect("Typed value cannot be coerced into a Boolean") as *const bool) iter.next().map_or(std::ptr::null_mut(), |v| &v.into_boolean().expect("Typed value cannot be coerced into a Boolean") as *const bool)
} }
//as_double //as_double
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_double(iter: *mut vec::IntoIter<TypedValue>) -> *const f64 { pub unsafe extern "C" fn values_iter_next_as_double(iter: *mut TypedValueIterator) -> *const f64 {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| &v.into_double().expect("Typed value cannot be coerced into a Double") as *const f64) iter.next().map_or(std::ptr::null_mut(), |v| &v.into_double().expect("Typed value cannot be coerced into a Double") as *const f64)
} }
//as_timestamp //as_timestamp
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_timestamp(iter: *mut vec::IntoIter<TypedValue>) -> *const i64 { pub unsafe extern "C" fn values_iter_next_as_timestamp(iter: *mut TypedValueIterator) -> *const i64 {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| v.into_timestamp().expect("Typed value cannot be coerced into a Timestamp") as *const i64) iter.next().map_or(std::ptr::null_mut(), |v| v.into_timestamp().expect("Typed value cannot be coerced into a Timestamp") as *const i64)
} }
//as_string //as_string
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_string(iter: *mut vec::IntoIter<TypedValue>) -> *const c_char { pub unsafe extern "C" fn values_iter_next_as_string(iter: *mut TypedValueIterator) -> *const c_char {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| c_char_from_rc(v.into_string().expect("Typed value cannot be coerced into a String"))) iter.next().map_or(std::ptr::null_mut(), |v| c_char_from_rc(v.into_string().expect("Typed value cannot be coerced into a String")))
} }
//as_uuid //as_uuid
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn values_iter_next_as_uuid(iter: *mut vec::IntoIter<TypedValue>) -> *const c_char { pub unsafe extern "C" fn values_iter_next_as_uuid(iter: *mut TypedValueIterator) -> *const c_char {
let iter = &mut *iter; let iter = &mut *iter;
iter.next().map_or(std::ptr::null_mut(), |v| string_to_c_char(v.into_uuid_string().expect("Typed value cannot be coerced into a Uuid"))) iter.next().map_or(std::ptr::null_mut(), |v| string_to_c_char(v.into_uuid_string().expect("Typed value cannot be coerced into a Uuid")))
} }
@ -556,11 +559,11 @@ pub unsafe extern "C" fn store_sync(store: *mut Store, user_uuid: *const c_char,
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
fn add_value_for_attribute<E, V>(store: &mut Store, entid: E, attribute: String, value: V) -> *mut ExternResult fn assert_datom<E, V>(store: &mut Store, entid: E, attribute: String, value: V) -> *mut ExternResult
where E: Into<KnownEntid>, where E: Into<KnownEntid>,
V: Into<TypedValue> { V: Into<TypedValue> {
let kw = kw_from_string(attribute); let kw = kw_from_string(attribute);
let res = store.add_value_for_attribute(entid.into(), kw, value.into()); let res = store.assert_datom(entid.into(), kw, value.into());
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
@ -568,7 +571,7 @@ where E: Into<KnownEntid>,
pub unsafe extern "C" fn store_set_long_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: i64) -> *mut ExternResult { pub unsafe extern "C" fn store_set_long_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: i64) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
let kw = kw_from_string(c_char_to_string(attribute)); let kw = kw_from_string(c_char_to_string(attribute));
let res = store.add_value_for_attribute(KnownEntid(entid), kw, TypedValue::Long(value)); let res = store.assert_datom(KnownEntid(entid), kw, TypedValue::Long(value));
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
@ -576,7 +579,7 @@ pub unsafe extern "C" fn store_set_long_for_attribute_on_entid(store: *mut Store
pub unsafe extern "C" fn store_set_entid_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: Entid) -> *mut ExternResult { pub unsafe extern "C" fn store_set_entid_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: Entid) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
let kw = kw_from_string(c_char_to_string(attribute)); let kw = kw_from_string(c_char_to_string(attribute));
let res = store.add_value_for_attribute(KnownEntid(entid), kw, TypedValue::Ref(value)); let res = store.assert_datom(KnownEntid(entid), kw, TypedValue::Ref(value));
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
@ -590,41 +593,41 @@ pub unsafe extern "C" fn store_set_kw_ref_for_attribute_on_entid(store: *mut Sto
return Box::into_raw(Box::new(ExternResult { ok: std::ptr::null_mut(), err: string_to_c_char(format!("Unknown attribute {:?}", value)) })); return Box::into_raw(Box::new(ExternResult { ok: std::ptr::null_mut(), err: string_to_c_char(format!("Unknown attribute {:?}", value)) }));
} }
let kw_entid = is_valid.unwrap(); let kw_entid = is_valid.unwrap();
let res = store.add_value_for_attribute(KnownEntid(entid), kw, TypedValue::Ref(kw_entid.into())); let res = store.assert_datom(KnownEntid(entid), kw, TypedValue::Ref(kw_entid.into()));
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn store_set_boolean_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: bool) -> *mut ExternResult { pub unsafe extern "C" fn store_set_boolean_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: bool) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
add_value_for_attribute(store, KnownEntid(entid), c_char_to_string(attribute), value) assert_datom(store, KnownEntid(entid), c_char_to_string(attribute), value)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn store_set_double_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: f64) -> *mut ExternResult { pub unsafe extern "C" fn store_set_double_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: f64) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
add_value_for_attribute(store, KnownEntid(entid), c_char_to_string(attribute), value) assert_datom(store, KnownEntid(entid), c_char_to_string(attribute), value)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn store_set_timestamp_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: time_t) -> *mut ExternResult { pub unsafe extern "C" fn store_set_timestamp_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: time_t) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
let kw = kw_from_string(c_char_to_string(attribute)); let kw = kw_from_string(c_char_to_string(attribute));
let res = store.add_value_for_attribute(KnownEntid(entid), kw, TypedValue::instant(value as i64)); let res = store.assert_datom(KnownEntid(entid), kw, TypedValue::instant(value as i64));
Box::into_raw(Box::new(res.into())) Box::into_raw(Box::new(res.into()))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn store_set_string_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: *const c_char) -> *mut ExternResult { pub unsafe extern "C" fn store_set_string_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: *const c_char) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
add_value_for_attribute(store, KnownEntid(entid), c_char_to_string(attribute), c_char_to_string(value)) assert_datom(store, KnownEntid(entid), c_char_to_string(attribute), c_char_to_string(value))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn store_set_uuid_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: *const c_char) -> *mut ExternResult { pub unsafe extern "C" fn store_set_uuid_for_attribute_on_entid(store: *mut Store, entid: Entid, attribute: *const c_char, value: *const c_char) -> *mut ExternResult {
let store = &mut*store; let store = &mut*store;
let uuid = Uuid::parse_str(&c_char_to_string(value)).expect("valid uuid"); let uuid = Uuid::parse_str(&c_char_to_string(value)).expect("valid uuid");
add_value_for_attribute(store, KnownEntid(entid), c_char_to_string(attribute), uuid) assert_datom(store, KnownEntid(entid), c_char_to_string(attribute), uuid)
} }
#[no_mangle] #[no_mangle]
@ -635,51 +638,24 @@ pub unsafe extern "C" fn destroy(obj: *mut c_void) {
} }
} }
#[no_mangle] macro_rules! define_destructor (
pub unsafe extern "C" fn query_builder_destroy(obj: *mut QueryBuilder) { ($name:ident, $t:ty) => (
if !obj.is_null() { #[no_mangle]
let _ = Box::from_raw(obj); pub unsafe extern "C" fn $name(obj: *mut $t) {
if !obj.is_null() { let _ = Box::from_raw(obj); }
} }
} )
);
define_destructor!(query_builder_destroy, QueryBuilder);
#[no_mangle] define_destructor!(store_destroy, Store);
pub unsafe extern "C" fn store_destroy(obj: *mut Store) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}
#[no_mangle] define_destructor!(typed_value_destroy, TypedValue);
pub unsafe extern "C" fn typed_value_destroy(obj: *mut TypedValue) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}
#[no_mangle] define_destructor!(typed_value_list_destroy, Vec<TypedValue>);
pub unsafe extern "C" fn typed_value_list_destroy(obj: *mut Vec<TypedValue>) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}
#[no_mangle] define_destructor!(typed_value_list_iter_destroy, TypedValueIterator);
pub unsafe extern "C" fn typed_value_list_iter_destroy(obj: *mut vec::IntoIter<TypedValue>) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}
#[no_mangle] define_destructor!(typed_value_result_set_destroy, Vec<Vec<TypedValue>>);
pub unsafe extern "C" fn typed_value_result_set_destroy(obj: *mut Vec<Vec<TypedValue>>) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}
#[no_mangle] define_destructor!(typed_value_result_set_iter_destroy, TypedValueListIterator);
pub unsafe extern "C" fn typed_value_result_set_iter_destroy(obj: *mut vec::IntoIter<Vec<TypedValue>>) {
if !obj.is_null() {
let _ = Box::from_raw(obj);
}
}

View file

@ -580,8 +580,8 @@ impl Store {
self.conn.unregister_observer(key); self.conn.unregister_observer(key);
} }
pub fn add_value_for_attribute<T>(&mut self, entid: T, attribute: NamespacedKeyword, value: TypedValue) -> Result<()> where T: Into<KnownEntid> { pub fn assert_datom<T>(&mut self, entid: T, attribute: NamespacedKeyword, value: TypedValue) -> Result<()> where T: Into<KnownEntid> {
self.conn.add_value_for_attribute(&mut self.sqlite, entid, attribute, value) self.conn.assert_datom(&mut self.sqlite, entid, attribute, value)
} }
} }
@ -874,7 +874,7 @@ impl Conn {
// TODO: expose the entity builder over FFI and remove the need for this function entirely // TODO: expose the entity builder over FFI and remove the need for this function entirely
// It's really only here in order to keep the FFI layer as thin as possible. // It's really only here in order to keep the FFI layer as thin as possible.
// Once the entity builder is exposed, we can perform all of these functions over FFI from the client. // Once the entity builder is exposed, we can perform all of these functions over FFI from the client.
pub fn add_value_for_attribute<T>(&mut self, sqlite: &mut rusqlite::Connection, entid: T, attribute: NamespacedKeyword, value: TypedValue) -> Result<()> where T: Into<KnownEntid> { pub fn assert_datom<T>(&mut self, sqlite: &mut rusqlite::Connection, entid: T, attribute: NamespacedKeyword, value: TypedValue) -> Result<()> where T: Into<KnownEntid> {
let in_progress = self.begin_transaction(sqlite)?; let in_progress = self.begin_transaction(sqlite)?;
let mut builder = in_progress.builder().describe(entid.into()); let mut builder = in_progress.builder().describe(entid.into());
builder.add_kw(&attribute, value)?; builder.add_kw(&attribute, value)?;