Skip to content
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

子struct内保存parent引用 #17

Open
wuyuedefeng opened this issue May 11, 2023 · 0 comments
Open

子struct内保存parent引用 #17

wuyuedefeng opened this issue May 11, 2023 · 0 comments

Comments

@wuyuedefeng
Copy link
Member

wuyuedefeng commented May 11, 2023

1. A raw ptr version:

child

struct Parent {
    name: String,
    child: Child,
}

struct Child {
    name: String,
    parent: *const Parent,
}

fn main() {
    let child = Child {
        name: "child".into(),
        parent: std::ptr::null(),
    };
    let mut parent = Parent { 
        name: "parent".into(),
        child,
    };
    parent.child.parent = &parent;
    println!("parent: {}", parent.name);
    println!("child: {}", parent.child.name);
}

children

struct Parent {
    name: String,
    children: Vec<Child>,
}

struct Child {
    name: String,
    parent: *const Parent,
}

fn main() {
    let mut parent = Parent { name: "parent".into(), children: Vec::new() };
    let child1 = Child { name: "child1".into(), parent: &parent };
    parent.children.push(child1);
    
    for child in parent.children.iter() {
        if !child.parent.is_null() {
            unsafe {
                println!("child: {}, parent: {}", child.name, (*(child.parent)).name);    
            }
        }
    }
}

Child loop nesting

struct Node {
    name: String,
    parent: *const Node,
    children: Vec<Node>,
}

fn main() {
    let mut parent = Node { name: "parent".into(), parent: std::ptr::null(), children: Vec::new() };
    let child1 = Node { name: "child1".into(), parent: &parent, children: Vec::new() };
    parent.children.push(child1);
    
    for child in parent.children.iter() {
        if !child.parent.is_null() {
            unsafe {
                println!("child: {}, parent: {}", child.name, (*(child.parent)).name);    
            }
        }
    }
}

2. A Rc version:

use std::rc::Rc;

// All Parent state that needs to be shared with Child goes here
struct Data {
    name: String
}

struct Parent {
    children: Vec<Child>,
    data: Rc<Data>,
}

struct Child {
    parent: Rc<Data>, // or Weak<Inner> if that's desirable
    data: Rc<Data>,
}

fn main() {
    let data = Rc::new(Data { name: "parent".into() });
    let mut parent = Parent {children: Vec::new(), data};
    let child = Child {parent: Rc::clone(&parent.data), data: Rc::new(Data { name: "child1".into() })};
    parent.children.push(child);
    
    for child in parent.children.iter() {
        println!("child: {}, parent: {}", child.data.name, child.parent.name);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant