难以实现自定义的入学人特征
我是生锈的新手,如果问题是天真的,请原谅我。
我正在尝试在Rust构建OS,并且正在关注 教程。操作系统还没有内存管理,因此目标是构建一个像向量一样的对象,因为它可以被推和弹出等,但它可以生存在堆栈上。我们通过用固定尺寸的数组初始化它来做到这一点。看起来像这样:
#[derive(Debug)]
pub struct StackVec<'a, T: 'a> {
storage: &'a mut [T],
len: usize
}
impl<'a, T: 'a> StackVec<'a, T> {
pub fn new(storage: &'a mut [T]) -> StackVec<'a, T> {
StackVec {
storage: storage,
len: 0,
}
}
pub fn with_len(storage: &'a mut [T], len: usize) -> StackVec<'a, T> {
if len > storage.len(){
panic!();
}
StackVec{
storage: storage,
len: len
}
}
pub fn capacity(&self) -> usize {
self.storage.len()
}
pub fn into_slice(self) -> &'a mut [T] {
&mut self.storage[0..self.len]
}
// Other functions which aren't relevant for the question.
}
弹出和推动增加并减少len
变量,并从数组中的适当位置添加和删除条目。
现在,我们还需要实现入学仪特征。鉴于stackvec
包含对数组的引用,我认为我可以从基础数组中返回迭代器:
impl <'a, T:'a> IntoIterator for StackVec<'a, T> {
type Item = T;
type IntoIter = core::array::IntoIter; // <- Throws "not found in `core::array"
fn into_iter(self) -> Self::IntoIter {
self.into_slice().into_iter()
}
}
但是,无论我处理多少编译。我找不到一种使用in_iter
的类型表达的方法,应返回数组的迭代器。我在做什么错?
I'm new to rust, so forgive me if the question is naive.
I'm trying to build an OS in rust and I'm following this tutorial. The OS doesn't have memory management yet, so the goal is to build an object which is like a vector in that it can be pushed and popped etc, but it lives on the stack. We do this by initializing it with an array of fixed size. It looks like this:
#[derive(Debug)]
pub struct StackVec<'a, T: 'a> {
storage: &'a mut [T],
len: usize
}
impl<'a, T: 'a> StackVec<'a, T> {
pub fn new(storage: &'a mut [T]) -> StackVec<'a, T> {
StackVec {
storage: storage,
len: 0,
}
}
pub fn with_len(storage: &'a mut [T], len: usize) -> StackVec<'a, T> {
if len > storage.len(){
panic!();
}
StackVec{
storage: storage,
len: len
}
}
pub fn capacity(&self) -> usize {
self.storage.len()
}
pub fn into_slice(self) -> &'a mut [T] {
&mut self.storage[0..self.len]
}
// Other functions which aren't relevant for the question.
}
Popping and pushing increases and decreases the len
variable and adds and removes entries from the appropriate place in the array.
Now, we also need to implement the IntoIterator trait. Given that the StackVec
contains a reference to an array, I thought that I could just return an iterator from the underlying array:
impl <'a, T:'a> IntoIterator for StackVec<'a, T> {
type Item = T;
type IntoIter = core::array::IntoIter; // <- Throws "not found in `core::array"
fn into_iter(self) -> Self::IntoIter {
self.into_slice().into_iter()
}
}
But no matter how much I play around with it, it still doesn't want to compile. I can't find a way to express using types that into_iter
should return the iterator for the array. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有不同的问题:
Array :: Intiterator
,因为您没有数组,所以有一个切片,这完全不同。例如,可以使用适当的core :: slice :: iter
如示例中解决。t
,但实际上您只能访问&amp; t
,因此返回item
应该是&amp; t
in_slice
方法使用&amp; mut
,这不是必需的,您可以重新安装此实现的存储空间。Different problems here:
array::IntoIterator
because you do not have an array, you have a slice, which is quite different. It can be solved, for example, by using the propercore::slice::Iter
as in the example.T
but in reality you only give access to&T
, so returnItem
should be&T
into_slice
method uses a&mut
which is not necessary, you can reslice the storage for this implementation.Playground