Take a C pointer, iterator over the characters, map the charactors to json which might mean quoting them string and escaping charactors.
Implement a printfunction that takes an Iterator that returns u8 and print the charactors, that can take (1) above.
Be able to collect the values from iterator and collect into a CString or
let cstr = CStr::from_bytes_with_nul(b"hello world\0");
let cstrptr = cstr.as_ptr();
printcstrptrfromiterator(CStrPtr::new(cstrptr).iter().tojson());
let X = CStrPtr::new(cstrptr).iter().tojson().collect(); # returns bytearray or CString
I currently have a CStrPtr struct that implements an Iterator trait.
I think what I have above gives me an iterator of the u8 in the C string pointer.
I need to implement tojson() that takes the iterator and maps the characters and possibly insert/return additional characters that turn the C String to a quoted json string.
And then collect the characters to return a CString or buffer/array.
I can't find good examples of how to do this.
I suspect tojson() would be a like the map() method.
Implement collect() that returns an array/CString.
You don't implement collect(); it is implemented automatically for FromIterator types.
Other than that, you could just use map, but it's not in general possible to return single characters mixed with escaped multi-character strings. You could use an enum:
Actually I am trying to implement a "Map" on an iterator, or function that takes an iterator and returns another iterator or modifies the iterator, to modify aka insert additional characters or modify existing characters as needed.
I am trying to figure out how to do that in Rust.
Filter takes and iterator and returns an iterator that skips some values, I guess I am trying to add, modify or delete elements from the iterator.
You don't have to re-implement map. You can just use the existing Iterator::map() implementation with whatever mapping function you provide to it. That's the point of it being generic over the function.
Isnt there no way to take an Iterator and "map like function" it into another Iterator, where the map like function, may add, modify or delete u8 from the input iterator and returns Iterator as well?