From ae9fa31b4c07dfe97f300e94e0afe9e58302f950 Mon Sep 17 00:00:00 2001 From: SunDoge <384813529@qq.com> Date: Thu, 24 Aug 2023 19:27:00 +0800 Subject: [PATCH] fix lifetime --- examples/issue_20.rs | 21 ++++++++++----------- src/ondemand/array.rs | 10 +++++----- src/ondemand/array_iterator.rs | 17 +++++++++-------- src/ondemand/field.rs | 12 ++++++------ src/ondemand/object.rs | 10 +++++----- src/ondemand/object_iterator.rs | 16 ++++++++-------- src/ondemand/value.rs | 10 +++++----- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/examples/issue_20.rs b/examples/issue_20.rs index 41e8357..8c96e02 100644 --- a/examples/issue_20.rs +++ b/examples/issue_20.rs @@ -7,20 +7,19 @@ fn main() -> Result<()> { let padded_string = make_padded_string(data); let mut doc = parser.iterate(&padded_string)?; - // let mut arr = doc.get_object()?.at_pointer("/ingredients")?.get_array()?; + let mut arr = doc.get_object()?.at_pointer("/ingredients")?.get_array()?; - // for value in arr.iter()? { - // let mut object = value?.get_object()?; + for value in arr.iter()? { + let mut object = value?.get_object()?; - // for field in object.iter()? { - // let mut field = field?; + for field in object.iter()? { + let mut field = field?; - - // let key = field.unescaped_key(false)?.to_owned(); - // let mut value = field.take_value(); - // println!("key: {} | value: {}", key, value.get_object()?.raw_json()?); - // } - // } + let key = field.unescaped_key(false)?.to_owned(); + let mut value = field.take_value(); + println!("key: {} | value: {}", key, value.get_object()?.raw_json()?); + } + } Ok(()) } diff --git a/src/ondemand/array.rs b/src/ondemand/array.rs index b142052..1de16dc 100644 --- a/src/ondemand/array.rs +++ b/src/ondemand/array.rs @@ -9,16 +9,16 @@ use crate::{ use super::parser::Parser; -pub struct Array<'a> { +pub struct Array { ptr: NonNull, - _document: PhantomData<&'a mut Document<'a, 'a>>, + // _document: PhantomData<&'a mut Document<'a>>, } -impl<'a> Array<'a> { +impl Array { pub fn new(ptr: NonNull) -> Self { Self { ptr, - _document: PhantomData, + // _document: PhantomData, } } @@ -73,4 +73,4 @@ impl<'a> Array<'a> { } } -impl_drop!(Array<'a>, ffi::SJ_OD_array_free); +impl_drop!(Array, ffi::SJ_OD_array_free); diff --git a/src/ondemand/array_iterator.rs b/src/ondemand/array_iterator.rs index c98075b..7d6d17f 100644 --- a/src/ondemand/array_iterator.rs +++ b/src/ondemand/array_iterator.rs @@ -5,14 +5,14 @@ use crate::{error::Result, macros::map_result}; use super::{array::Array, document::Document, value::Value}; -pub struct ArrayIterator<'a> { +pub struct ArrayIterator { begin: NonNull, end: NonNull, running: bool, - _array: PhantomData<&'a mut Array<'a>>, + // _array: PhantomData<&'a mut Array>, } -impl<'a> ArrayIterator<'a> { +impl ArrayIterator { pub fn new( begin: NonNull, end: NonNull, @@ -21,11 +21,11 @@ impl<'a> ArrayIterator<'a> { begin, end, running: false, - _array: PhantomData, + // _array: PhantomData, } } - pub fn get(&mut self) -> Result> { + pub fn get(&mut self) -> Result { map_result!( ffi::SJ_OD_array_iterator_get(self.begin.as_mut()), ffi::SJ_OD_value_result_error, @@ -43,7 +43,7 @@ impl<'a> ArrayIterator<'a> { } } -impl<'a> Drop for ArrayIterator<'a> { +impl Drop for ArrayIterator { fn drop(&mut self) { unsafe { ffi::SJ_OD_array_iterator_free(self.begin.as_mut()); @@ -52,8 +52,8 @@ impl<'a> Drop for ArrayIterator<'a> { } } -impl<'a> Iterator for ArrayIterator<'a> { - type Item = Result>; +impl Iterator for ArrayIterator { + type Item = Result; fn next(&mut self) -> Option { if self.running { @@ -81,6 +81,7 @@ mod tests { let mut parser = Parser::default(); let ps = make_padded_string("[1,2,3]"); let mut doc = parser.iterate(&ps).unwrap(); + // drop(ps); let mut arr = doc.get_array().unwrap(); for (v, num) in arr.iter().unwrap().zip([1u64, 2, 3]) { let res = v.unwrap().get_uint64().unwrap(); diff --git a/src/ondemand/field.rs b/src/ondemand/field.rs index 616a425..0b86ab7 100644 --- a/src/ondemand/field.rs +++ b/src/ondemand/field.rs @@ -9,16 +9,16 @@ use crate::utils::string_view_to_str; use super::object::Object; use super::value::Value; -pub struct Field<'a> { +pub struct Field { ptr: NonNull, - _object: PhantomData<&'a mut Object<'a>>, + // _object: PhantomData<&'a mut Object>, } -impl<'a> Field<'a> { +impl Field { pub fn new(ptr: NonNull) -> Self { Self { ptr, - _object: PhantomData, + // _object: PhantomData, } } @@ -50,7 +50,7 @@ impl<'a> Field<'a> { // Value::new(ptr) // } - pub fn take_value(self) -> Value<'a> { + pub fn take_value(self) -> Value { let ptr = unsafe { let ptr = ffi::SJ_OD_field_take_value(self.ptr.as_ptr()); NonNull::new_unchecked(ptr) @@ -60,4 +60,4 @@ impl<'a> Field<'a> { } } -impl_drop!(Field<'a> , ffi::SJ_OD_field_free); +impl_drop!(Field, ffi::SJ_OD_field_free); diff --git a/src/ondemand/object.rs b/src/ondemand/object.rs index 4ae17bf..d3a4282 100644 --- a/src/ondemand/object.rs +++ b/src/ondemand/object.rs @@ -10,16 +10,16 @@ use super::parser::Parser; use super::value::Value; use crate::error::Result; -pub struct Object<'a> { +pub struct Object { ptr: NonNull, - _document: PhantomData<&'a mut Document<'a, 'a>>, + // _document: PhantomData<&'a mut Document<'a, 'a>>, } -impl<'a> Object<'a> { +impl Object { pub fn new(ptr: NonNull) -> Self { Self { ptr, - _document: PhantomData, + // _document: PhantomData, } } @@ -60,4 +60,4 @@ impl<'a> Object<'a> { } } -impl_drop!(Object<'a>, ffi::SJ_OD_object_free); +impl_drop!(Object, ffi::SJ_OD_object_free); diff --git a/src/ondemand/object_iterator.rs b/src/ondemand/object_iterator.rs index 4934c9e..2c9fe63 100644 --- a/src/ondemand/object_iterator.rs +++ b/src/ondemand/object_iterator.rs @@ -5,14 +5,14 @@ use crate::{error::Result, macros::map_result}; use super::{field::Field, object::Object, value::Value}; -pub struct ObjectIterator<'a> { +pub struct ObjectIterator { begin: NonNull, end: NonNull, running: bool, - _object: PhantomData<&'a mut Object<'a>>, + // _object: PhantomData<&'a mut Object>, } -impl<'a> ObjectIterator<'a> { +impl ObjectIterator { pub fn new( begin: NonNull, end: NonNull, @@ -21,11 +21,11 @@ impl<'a> ObjectIterator<'a> { begin, end, running: false, - _object: PhantomData, + // _object: PhantomData, } } - pub fn get(&mut self) -> Result> { + pub fn get(&mut self) -> Result { map_result!( ffi::SJ_OD_object_iterator_get(self.begin.as_mut()), ffi::SJ_OD_field_result_error, @@ -43,7 +43,7 @@ impl<'a> ObjectIterator<'a> { } } -impl<'a> Drop for ObjectIterator<'a> { +impl Drop for ObjectIterator { fn drop(&mut self) { unsafe { ffi::SJ_OD_object_iterator_free(self.begin.as_mut()); @@ -52,8 +52,8 @@ impl<'a> Drop for ObjectIterator<'a> { } } -impl<'a> Iterator for ObjectIterator<'a> { - type Item = Result>; +impl Iterator for ObjectIterator { + type Item = Result; fn next(&mut self) -> Option { if self.running { diff --git a/src/ondemand/value.rs b/src/ondemand/value.rs index 960f6ce..dd5c28b 100644 --- a/src/ondemand/value.rs +++ b/src/ondemand/value.rs @@ -7,16 +7,16 @@ use super::document::Document; use super::{array::Array, object::Object}; use crate::error::Result; -pub struct Value<'a> { +pub struct Value { ptr: NonNull, - _document: PhantomData<&'a mut Document<'a, 'a>>, + // _document: PhantomData<&'a mut Document<'a, 'a>>, } -impl<'a> Value<'a> { +impl Value { pub fn new(ptr: NonNull) -> Self { Self { ptr, - _document: PhantomData, + // _document: PhantomData, } } @@ -75,4 +75,4 @@ impl<'a> Value<'a> { } } -impl_drop!(Value<'a>, ffi::SJ_OD_value_free); +impl_drop!(Value, ffi::SJ_OD_value_free);