Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(arbitrary_self_types)]
- use std::marker::PhantomData;
- use std::ptr::NonNull;
- #[allow(non_camel_case_types)]
- type gpointer = *mut std::ffi::c_void;
- pub struct GList {
- pub data: gpointer,
- pub next: *mut GList,
- pub prev: *mut GList,
- }
- impl GList {
- pub fn iter<T>(self: *mut Self) -> GListIter<T> {
- GListIter {
- glist: NonNull::new(self),
- _marker: PhantomData,
- }
- }
- }
- pub struct GListIter<T> {
- glist: Option<NonNull<GList>>,
- _marker: PhantomData<T>,
- }
- impl<T> Iterator for GListIter<T> {
- type Item = *mut T;
- fn next(&mut self) -> Option<Self::Item> {
- self.glist.map(|current| {
- self.glist = NonNull::new(unsafe { (*current.as_ptr()).next });
- unsafe { (*current.as_ptr()).data as *mut T }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement