-
Notifications
You must be signed in to change notification settings - Fork 29
Add RuntimeVariableList and RuntimeFixedVector
#67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
macladson
wants to merge
7
commits into
sigp:main
Choose a base branch
from
macladson:add-runtime-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83b3516
Add runtime_types
macladson 32c8552
Merge branch 'main' into add-runtime-types
macladson 6b5c003
Merge branch 'main' into add-runtime-types
macladson a8db9c1
Feature gate context_deserialize impl
macladson bbd97c4
Fix feature deps
macladson 06ad5c4
Merge branch 'main' into add-runtime-types
macladson 663aaac
Merge branch 'main' into add-runtime-types
michaelsproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||||||
| use crate::RuntimeVariableList; | ||||||
| use context_deserialize::ContextDeserialize; | ||||||
| use serde::{de::Error as DeError, Deserializer}; | ||||||
|
|
||||||
| impl<'de, C, T> ContextDeserialize<'de, (C, usize)> for RuntimeVariableList<T> | ||||||
| where | ||||||
| T: ContextDeserialize<'de, C>, | ||||||
| C: Clone, | ||||||
| { | ||||||
| fn context_deserialize<D>(deserializer: D, context: (C, usize)) -> Result<Self, D::Error> | ||||||
| where | ||||||
| D: Deserializer<'de>, | ||||||
| { | ||||||
| // First parse out a Vec<C> using the Vec<C> impl you already have. | ||||||
|
||||||
| // First parse out a Vec<C> using the Vec<C> impl you already have. | |
| // First parse out a Vec<T> using the Vec<T> impl you already have. |
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird way to phrase this, but it's right
This file contains hidden or 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,7 @@ | ||
| #[cfg(feature = "context_deserialize")] | ||
| mod context_deserialize; | ||
| mod runtime_fixed_vector; | ||
| mod runtime_variable_list; | ||
|
|
||
| pub use runtime_fixed_vector::RuntimeFixedVector; | ||
| pub use runtime_variable_list::RuntimeVariableList; |
This file contains hidden or 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,90 @@ | ||
| //! Emulates a fixed size array but with the length set at runtime. | ||
| //! | ||
| //! The length of the list cannot be changed once it is set. | ||
|
|
||
| use std::fmt; | ||
| use std::fmt::Debug; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct RuntimeFixedVector<T> { | ||
| vec: Vec<T>, | ||
| len: usize, | ||
| } | ||
|
|
||
| impl<T: Debug> Debug for RuntimeFixedVector<T> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{:?} (len={})", self.vec, self.len) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Clone + Default> RuntimeFixedVector<T> { | ||
| pub fn new(vec: Vec<T>) -> Self { | ||
| let len = vec.len(); | ||
| Self { vec, len } | ||
| } | ||
|
|
||
| pub fn to_vec(&self) -> Vec<T> { | ||
| self.vec.clone() | ||
| } | ||
|
|
||
| pub fn as_slice(&self) -> &[T] { | ||
| self.vec.as_slice() | ||
| } | ||
|
|
||
| #[allow(clippy::len_without_is_empty)] | ||
| pub fn len(&self) -> usize { | ||
| self.len | ||
| } | ||
|
|
||
| pub fn into_vec(self) -> Vec<T> { | ||
| self.vec | ||
| } | ||
|
|
||
| pub fn default(max_len: usize) -> Self { | ||
| Self { | ||
| vec: vec![T::default(); max_len], | ||
| len: max_len, | ||
| } | ||
| } | ||
|
|
||
| pub fn take(&mut self) -> Self { | ||
| let new = std::mem::take(&mut self.vec); | ||
| *self = Self::new(vec![T::default(); self.len]); | ||
| Self { | ||
| vec: new, | ||
| len: self.len, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> std::ops::Deref for RuntimeFixedVector<T> { | ||
| type Target = [T]; | ||
|
|
||
| fn deref(&self) -> &[T] { | ||
| &self.vec[..] | ||
| } | ||
| } | ||
|
|
||
| impl<T> std::ops::DerefMut for RuntimeFixedVector<T> { | ||
| fn deref_mut(&mut self) -> &mut [T] { | ||
| &mut self.vec[..] | ||
| } | ||
| } | ||
|
|
||
| impl<T> IntoIterator for RuntimeFixedVector<T> { | ||
| type Item = T; | ||
| type IntoIter = std::vec::IntoIter<T>; | ||
|
|
||
| fn into_iter(self) -> Self::IntoIter { | ||
| self.vec.into_iter() | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T> IntoIterator for &'a RuntimeFixedVector<T> { | ||
| type Item = &'a T; | ||
| type IntoIter = std::slice::Iter<'a, T>; | ||
|
|
||
| fn into_iter(self) -> Self::IntoIter { | ||
| self.vec.iter() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.