图形结构中的拥有指针

在Rust社区的慷慨帮助下,我设法获得了使用托管指针组装的拓扑数据结构的基础.这很好地结合在一起,我对Rust一般感到非常兴奋.然后我读了这个
post(这似乎是一个合理的计划),它激励我回溯并尝试使用自有指针重新组装它,如果可能的话.

这是使用托管指针的工作版本:

struct Dart<T> {
    alpha: ~[@mut Dart<T>],
    embed: ~[@mut T],
   tagged: bool
}

impl<T> Dart<T> {
    pub fn new(dim: uint) -> @mut Dart<T> {
        let mut dart = @mut Dart{alpha: ~[], embed: ~[], tagged: false};        
        dart.alpha = vec::from_elem(dim, dart);              
        return dart;
    }
    pub fn get_dim(&self) -> uint {
        return self.alpha.len();
    }   
    pub fn traverse(@mut self, invs: &[uint], f: &fn(&Dart<T>)) {
        let dim = self.get_dim();
        for invs.each |i| {if *i >= dim {return}}; //test bounds on invs vec
        if invs.len() == 2 {
            let spread:int = int::abs(invs[1] as int - invs[0] as int);
            if spread == 1 { //simple loop
                let mut dart = self;
                let mut i = invs[0];
                while !dart.tagged {
                    dart.tagged = true; 
                    f(dart);
                    dart = dart.alpha[i];
                    if i == invs[0] {i = invs[1];}
                    else {i == invs[0];}
            }   }           
            // else if spread == 2 { // max 4 cells traversed

            // }
        }
        else {
            let mut stack = ~[self];
            self.tagged = true;     
            while !stack.is_empty() {
                let mut dart = stack.pop();
                f(dart);  
                for invs.each |i| {
                    if !dart.alpha[*i].tagged {
                        dart.alpha[*i].tagged = true;
                        stack.push(dart);
}   }   }   }   }   }    

经过几个小时追逐终身错误后,我得出的结论是,由于循环性质,这可能甚至不可能由于拥有指针(没有打结,因为我是warned).我对此的微弱尝试如下.我的问题是,这种结构是否可以在不借助托管指针的情况下实现?如果没有,上面的代码是否被认为是“生锈的”? (惯用的锈).谢谢.

struct GMap<'self,T> {
    dim: uint,
    darts: ~[~Dart<'self,T>]
}   

struct Dart<'self,T> { 
    alpha: ~[&'self mut Dart<'self, T>], 
    embed: ~[&'self mut T], 
    tagged: bool 
}

impl<'self, T> GMap<'self, T> {
    pub fn new_dart(&'self mut self) {
        let mut dart = ~Dart{alpha: ~[], embed: ~[], tagged: false};
        let dartRef: &'self mut Dart<'self, T> = dart;  
        dartRef.alpha = vec::from_elem(self.dim, copy dartRef);
        self.darts.push(dart);              
    }
} 

最佳答案 我很确定使用& mut指针是不可能的,因为一次只能存在一个这样的指针,例如:

fn main() {
    let mut i = 0;
    let a = &mut i;
    let b = &mut i;
}
and-mut.rs:4:12: 4:18 error: cannot borrow `i` as mutable more than once at at a time
and-mut.rs:4     let b = &mut i;
                         ^~~~~~
and-mut.rs:3:12: 3:18 note: second borrow of `i` as mutable occurs here
and-mut.rs:3     let a = &mut i;
                         ^~~~~~
error: aborting due to previous error

通过将不安全的指针存储到内存(ptr :: to_mut_unsafe_ptr)或将索引存储到GMap的飞镖成员中,可以不安全地绕过借用检查器.本质上,存储对内存的单个引用(在self.darts中)并且所有操作都必须通过它.

这可能看起来像:

impl<'self, T> GMap<'self, T> {
    pub fn new_dart(&'self mut self) {
        let ind = self.darts.len();
        self.darts.push(~Dart{alpha: vec::from_elem(self.dim, ind), embed: ~[], tagged: false});
    }
} 

遍历将需要改变为GMap上的方法(例如fn(& mut self,node_ind:uint,invs:& [uint],f:& fn(& Dart< T>))),或者至少采用GMap类型.

(完全不同的是,有一个对external iterators的库支持,它比内部迭代器(采用闭包的那些)更具组合性.因此,为遍历定义其中一个可能(或可能不)使其更好地使用它. )

点赞