-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ManagedVec iterator refactor, ManagedVecPayloadIterator
- Loading branch information
1 parent
1db87bb
commit 0df3c0a
Showing
4 changed files
with
134 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use core::marker::PhantomData; | ||
|
||
use crate::api::{ManagedBufferApiImpl, ManagedTypeApi}; | ||
|
||
use super::ManagedVecItemPayload; | ||
|
||
pub struct ManagedVecPayloadIterator<M, P> | ||
where | ||
M: ManagedTypeApi, | ||
P: ManagedVecItemPayload, | ||
{ | ||
pub(super) vec_handle: M::ManagedBufferHandle, | ||
byte_start: usize, | ||
byte_end: usize, | ||
_phantom: PhantomData<P>, | ||
} | ||
|
||
impl<M, P> ManagedVecPayloadIterator<M, P> | ||
where | ||
M: ManagedTypeApi, | ||
P: ManagedVecItemPayload, | ||
{ | ||
/// Unsafe because it works with the managed vec handle directly, so does not take ownership into account. | ||
pub(crate) unsafe fn new(vec_handle: M::ManagedBufferHandle) -> Self { | ||
let byte_end = M::managed_type_impl().mb_len(vec_handle.clone()); | ||
ManagedVecPayloadIterator { | ||
vec_handle, | ||
byte_start: 0, | ||
byte_end, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
/// Unsafe because it works with the managed vec handle directly, so does not take ownership into account. | ||
pub(super) unsafe fn clone_iter(&self) -> Self { | ||
ManagedVecPayloadIterator { | ||
vec_handle: self.vec_handle.clone(), | ||
byte_start: self.byte_start, | ||
byte_end: self.byte_end, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<M, P> Iterator for ManagedVecPayloadIterator<M, P> | ||
where | ||
M: ManagedTypeApi, | ||
P: ManagedVecItemPayload, | ||
{ | ||
type Item = P; | ||
|
||
fn next(&mut self) -> Option<P> { | ||
let next_byte_start = self.byte_start + P::payload_size(); | ||
if next_byte_start > self.byte_end { | ||
return None; | ||
} | ||
|
||
let mut payload = P::new_buffer(); | ||
let _ = M::managed_type_impl().mb_load_slice( | ||
self.vec_handle.clone(), | ||
self.byte_start, | ||
payload.payload_slice_mut(), | ||
); | ||
|
||
self.byte_start = next_byte_start; | ||
Some(payload) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let size = P::payload_size(); | ||
let remaining = (self.byte_end - self.byte_start) / size; | ||
(remaining, Some(remaining)) | ||
} | ||
} | ||
|
||
impl<M, P> ExactSizeIterator for ManagedVecPayloadIterator<M, P> | ||
where | ||
M: ManagedTypeApi, | ||
P: ManagedVecItemPayload, | ||
{ | ||
} | ||
|
||
impl<M, P> DoubleEndedIterator for ManagedVecPayloadIterator<M, P> | ||
where | ||
M: ManagedTypeApi, | ||
P: ManagedVecItemPayload, | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
if self.byte_start + P::payload_size() > self.byte_end { | ||
return None; | ||
} | ||
self.byte_end -= P::payload_size(); | ||
|
||
let mut payload = P::new_buffer(); | ||
let _ = M::managed_type_impl().mb_load_slice( | ||
self.vec_handle.clone(), | ||
self.byte_end, | ||
payload.payload_slice_mut(), | ||
); | ||
|
||
Some(payload) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters