繁体   English   中英

线程“main”在 Rust 中溢出了它的堆栈

[英]thread 'main' has overflowed its stack in Rust

我正在尝试学习 Rust(我来自 Java),但遇到了一些问题。 我正在构建一个简单的程序,它是连接池的基础。 当我运行它时,我得到运行时错误thread 'main' has overflowed its stack ,我不明白为什么。

这是 main.rs

use hello_rust::concurrent_bag;
use hello_rust::concurrent_bag::BagEntry;

fn main() {
    let my_bagentry = BagEntry::new(String::from("ciao"));
    //println!("{}", my_bagentry.value());
    let mut contVec : Vec<BagEntry<String>>=vec![];
    contVec.push(my_bagentry);
    println!("state ={}", contVec[0]);
    println!("state ={}", contVec[0].value());
    let mut my_bag: concurrent_bag::ConcurrentBag<String> =concurrent_bag::ConcurrentBag::new();
    my_bag.addEntry(String::from("ciao Entry"));
    let result = my_bag.borrowEntry();
    if result.is_some() {
    println!("{}", result.unwrap().value());
    }
}

这是 lib.rs

pub mod concurrent_bag {
    use crate::concurrent_bag::BagEntryState::UNUSED;

    pub enum BagEntryState {
        UNUSED, USED, REMOVED
    }

    impl fmt::Display for BagEntryState {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match *self {
                BagEntryState::UNUSED => write!(f, "UNUSED"),
                BagEntryState::USED => write!(f, "USED"),
                BagEntryState::REMOVED => write!(f, "REMOVED"),
            }
        }
    }

    impl PartialEq for BagEntryState {
        fn eq(&self, other: &Self) -> bool {
            self == other
        }
    }

    pub struct BagEntry< T: std::cmp::PartialEq + fmt::Display> {
        state : BagEntryState,
        value: T,
    }

    impl<'a, T: std::cmp::PartialEq + fmt::Display> BagEntry<T> {
        pub fn new(value: T) -> BagEntry< T> {
            BagEntry {
                value,
                state: UNUSED,
            }
        }

        pub fn value(&self)->&T {
            &self.value
        }
        pub fn state(&self)->&BagEntryState {
            &self.state
        }
    }

    impl<'a, T: std::cmp::PartialEq + fmt::Display> PartialEq for BagEntry<T> {
        fn eq(&self, other: &Self) -> bool {
            self.value == other.value
        }
    }

    impl<T: std::cmp::PartialEq + fmt::Display> fmt::Display for BagEntry<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", self.value)
        }
    }

    use std::sync::Arc;
    use core::fmt;

    pub struct ConcurrentBag<T: std::cmp::PartialEq + fmt::Display> {
        entry_list:Vec<BagEntry<T>>,
    }

    impl<'a, T:  std::cmp::PartialEq + fmt::Display> ConcurrentBag<T> {
        pub fn new() -> ConcurrentBag<T> {
            ConcurrentBag {
                entry_list: vec![],
            }
        }

        pub fn borrowEntry(&self) -> Option<BagEntry<T>> {
            println!("borrow vc size {}", self.entry_list.len());
            println!("value ={}", (self).entry_list[0].value());
            println!("state ={}", (self).entry_list[0].state());
            if (self).entry_list[0].state()==&UNUSED {

            }
            let result:Option<BagEntry<T>> =None;
            result
        }

        pub fn addEntry(&mut self, value: T) {
            let my_bagentry = BagEntry::new(value);
            (*self).entry_list.push(my_bagentry);
            println!("addEntry vc size {}", self.entry_list.len())
        }

        pub fn removeEntry(&mut self, value: T) {
            let my_bagentry = BagEntry::new(value);
            let index =(*self).entry_list.iter().position(|x| *x == my_bagentry).unwrap();
            self.entry_list.remove(index);
        }
    }
}

有问题的线路是

if (self).entry_list[0].state()==&UNUSED

我无法理解为什么因为这条线

println!("state ={}", (self).entry_list[0].state());

似乎运作良好。 另一个让我困惑的问题是,如果我在borrowEntry 中使用&self ,我应该使用*self来访问entry_list但程序编译并运行时没有错误。

这段代码不会永远重复自己吗?

impl PartialEq for BagEntryState {
    fn eq(&self, other: &Self) -> bool {
        self == other
    }
}

我们实现PartialEq以使用==!=运算符来比较类型的实例。 所以,在那里使用self == other是没有意义的。

你可以推导出PartialEq

#[derive(PartialEq)]
pub enum BagEntryState {
    UNUSED,
    USED,
    REMOVED,
}

或者,如果你想手动实现它,你可以做这样的事情。

impl PartialEq for BagEntryState {
    fn eq(&self, other: &BagEntryState) -> bool {
        match (self, other) {
            (&BagEntryState::UNUSED, &BagEntryState::UNUSED)
            | (&BagEntryState::USED, &BagEntryState::USED)
            | (&BagEntryState::REMOVED, &BagEntryState::REMOVED) => true,
            _ => false,
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM