难以实现自定义的入学人特征

发布于 2025-01-28 13:03:22 字数 1591 浏览 3 评论 0原文

我是生锈的新手,如果问题是天真的,请原谅我。

我正在尝试在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_itershould return the iterator for the array. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-02-04 13:03:22

这里有不同的问题:

  • 您不能使用Array :: Intiterator,因为您没有数组,所以有一个切片,这完全不同。例如,可以使用适当的core :: slice :: iter如示例中解决。
  • 您正在尝试返回t,但实际上您只能访问&amp; t,因此返回item应该是&amp; t
  • 您的in_slice方法使用&amp; mut,这不是必需的,您可以重新安装此实现的存储空间。
impl <'a, T:'a> IntoIterator for StackVec<'a, T> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage[0..self.len].into_iter()
    }
}

Different problems here:

  • You cannot use 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 proper core::slice::Iter as in the example.
  • You are trying to return T but in reality you only give access to &T, so return Item should be &T
  • Your into_slice method uses a &mut which is not necessary, you can reslice the storage for this implementation.
impl <'a, T:'a> IntoIterator for StackVec<'a, T> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage[0..self.len].into_iter()
    }
}

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文