-
Notifications
You must be signed in to change notification settings - Fork 49
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
Suggestion: Default-less safe implementation with small overhead #183
Comments
yeah, I faced with the impossibility to make |
To reuse good solutions from tinyvec, one could use something like that: use tinyvec::{tiny_vec, TinyVec};
const LIMIT: usize = 2;
#[derive(Default, Debug, Clone)]
pub struct OptTinyVec<T> {
inner: TinyVec<[Option<T>; LIMIT]>,
}
impl<T> OptTinyVec<T> {
#[inline]
pub fn push(&mut self, value: T) {
self.inner.push(Some(value));
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item=&T> + '_ {
self.inner
.iter()
.map(|it| it.as_ref().unwrap())
}
} I think it's ok to have this on the user project level. Of course, that would be nice to have something like that as library, but LIMIT is hardcoded and cannot be parameterized easily. The only approach I see is to generate OptArray impls like Array impls are generated. It means extra efforts to synchronize it with tinyvec. I'm not sure that worth... |
Hi. I just had an idea that there could be and option to avoid Default bound on user side by using Option to wrap items under the hood. Of course, that's additional overhead, but seems like the whole point of TinyVec is to be optimal in cases when size is small, when such overhead seems acceptable. Maybe worth to have it as additional type?
The text was updated successfully, but these errors were encountered: