Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #![feature(arbitrary_self_types)]
  2.  
  3. use std::marker::PhantomData;
  4. use std::ptr::NonNull;
  5.  
  6. #[allow(non_camel_case_types)]
  7. type gpointer = *mut std::ffi::c_void;
  8.  
  9. pub struct GList {
  10. pub data: gpointer,
  11. pub next: *mut GList,
  12. pub prev: *mut GList,
  13. }
  14.  
  15. impl GList {
  16. pub fn iter<T>(self: *mut Self) -> GListIter<T> {
  17. GListIter {
  18. glist: NonNull::new(self),
  19. _marker: PhantomData,
  20. }
  21. }
  22. }
  23.  
  24. pub struct GListIter<T> {
  25. glist: Option<NonNull<GList>>,
  26. _marker: PhantomData<T>,
  27. }
  28.  
  29. impl<T> Iterator for GListIter<T> {
  30. type Item = *mut T;
  31.  
  32. fn next(&mut self) -> Option<Self::Item> {
  33. self.glist.map(|current| {
  34. self.glist = NonNull::new(unsafe { (*current.as_ptr()).next });
  35.  
  36. unsafe { (*current.as_ptr()).data as *mut T }
  37. })
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement