Skip to content

Commit

Permalink
fix lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
SunDoge committed Aug 24, 2023
1 parent 4562326 commit ae9fa31
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
21 changes: 10 additions & 11 deletions examples/issue_20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
10 changes: 5 additions & 5 deletions src/ondemand/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use crate::{

use super::parser::Parser;

pub struct Array<'a> {
pub struct Array {
ptr: NonNull<ffi::SJ_OD_array>,
_document: PhantomData<&'a mut Document<'a, 'a>>,
// _document: PhantomData<&'a mut Document<'a>>,
}

impl<'a> Array<'a> {
impl Array {
pub fn new(ptr: NonNull<ffi::SJ_OD_array>) -> Self {
Self {
ptr,
_document: PhantomData,
// _document: PhantomData,
}
}

Expand Down Expand Up @@ -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);
17 changes: 9 additions & 8 deletions src/ondemand/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi::SJ_OD_array_iterator>,
end: NonNull<ffi::SJ_OD_array_iterator>,
running: bool,
_array: PhantomData<&'a mut Array<'a>>,
// _array: PhantomData<&'a mut Array>,
}

impl<'a> ArrayIterator<'a> {
impl ArrayIterator {
pub fn new(
begin: NonNull<ffi::SJ_OD_array_iterator>,
end: NonNull<ffi::SJ_OD_array_iterator>,
Expand All @@ -21,11 +21,11 @@ impl<'a> ArrayIterator<'a> {
begin,
end,
running: false,
_array: PhantomData,
// _array: PhantomData,
}
}

pub fn get(&mut self) -> Result<Value<'a>> {
pub fn get(&mut self) -> Result<Value> {
map_result!(
ffi::SJ_OD_array_iterator_get(self.begin.as_mut()),
ffi::SJ_OD_value_result_error,
Expand All @@ -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());
Expand All @@ -52,8 +52,8 @@ impl<'a> Drop for ArrayIterator<'a> {
}
}

impl<'a> Iterator for ArrayIterator<'a> {
type Item = Result<Value<'a>>;
impl Iterator for ArrayIterator {
type Item = Result<Value>;

fn next(&mut self) -> Option<Self::Item> {
if self.running {
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/ondemand/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi::SJ_OD_field>,
_object: PhantomData<&'a mut Object<'a>>,
// _object: PhantomData<&'a mut Object>,
}

impl<'a> Field<'a> {
impl Field {
pub fn new(ptr: NonNull<ffi::SJ_OD_field>) -> Self {
Self {
ptr,
_object: PhantomData,
// _object: PhantomData,
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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);
10 changes: 5 additions & 5 deletions src/ondemand/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi::SJ_OD_object>,
_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<ffi::SJ_OD_object>) -> Self {
Self {
ptr,
_document: PhantomData,
// _document: PhantomData,
}
}

Expand Down Expand Up @@ -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);
16 changes: 8 additions & 8 deletions src/ondemand/object_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi::SJ_OD_object_iterator>,
end: NonNull<ffi::SJ_OD_object_iterator>,
running: bool,
_object: PhantomData<&'a mut Object<'a>>,
// _object: PhantomData<&'a mut Object>,
}

impl<'a> ObjectIterator<'a> {
impl ObjectIterator {
pub fn new(
begin: NonNull<ffi::SJ_OD_object_iterator>,
end: NonNull<ffi::SJ_OD_object_iterator>,
Expand All @@ -21,11 +21,11 @@ impl<'a> ObjectIterator<'a> {
begin,
end,
running: false,
_object: PhantomData,
// _object: PhantomData,
}
}

pub fn get(&mut self) -> Result<Field<'a>> {
pub fn get(&mut self) -> Result<Field> {
map_result!(
ffi::SJ_OD_object_iterator_get(self.begin.as_mut()),
ffi::SJ_OD_field_result_error,
Expand All @@ -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());
Expand All @@ -52,8 +52,8 @@ impl<'a> Drop for ObjectIterator<'a> {
}
}

impl<'a> Iterator for ObjectIterator<'a> {
type Item = Result<Field<'a>>;
impl Iterator for ObjectIterator {
type Item = Result<Field>;

fn next(&mut self) -> Option<Self::Item> {
if self.running {
Expand Down
10 changes: 5 additions & 5 deletions src/ondemand/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi::SJ_OD_value>,
_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<ffi::SJ_OD_value>) -> Self {
Self {
ptr,
_document: PhantomData,
// _document: PhantomData,
}
}

Expand Down Expand Up @@ -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);

0 comments on commit ae9fa31

Please sign in to comment.