< 返回版块

LayneYy 发表于 2020-03-13 17:06

mod repository;
mod core;

use log::{info, warn, error};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use crate::repository::user::User;
use std::collections::LinkedList;
use mysql::serde::export::Option::Some;
use mysql::{PooledConn, Pool};

struct NodeData {
    pub user: User,
    pub profit: Mutex<Option<f64>>,
}

fn main() {
    let pool = repository::get_pool();
    let arc_pool = Arc::new(pool);
    let users = User::find_all_manager(arc_pool.get_conn().unwrap());
    let roots: Vec<LinkedList<Arc<NodeData>>> = users
        .into_iter()
        .map(|mu| {
            let mut l = LinkedList::new();
            l.push_back(Arc::new(NodeData { user: mu, profit: Mutex::new(None) }));
            l
        })
        .collect();
    for root in roots {
        let vec = create_child(root, arc_pool.clone());
        println!("size of vec is {}", vec.len());
    }
}
//这里一个链表,复用前面的的节点数据,根据get_invitees分裂出新的链表
fn create_child(mut root: LinkedList<Arc<NodeData>>, mut pool: Arc<Pool>) -> Vec<LinkedList<Arc<NodeData>>> {
    let data = root.back().expect("未获取到尾部元素");
    if let Some(invitees) = data.user.get_invitees(pool.get_conn().unwrap()) {
        invitees
            .into_iter()
            .flat_map(|inv| {
                let mut child_list = LinkedList::new();
                for n in root.iter() {
                    child_list.push_back(n.clone());
                }
                let node_data = NodeData { user: inv, profit: Mutex::new(None) };
                child_list.push_back(Arc::new(node_data));
                create_child(child_list, pool.clone())
            })
            .collect()
    } else {
        vec![root]
    }
}

打印出来 打印出来的size 全是0

评论区

写评论
作者 LayneYy 2020-03-16 20:59

老板是因为 invitess 返回了一个empty Vec 对以下内容的回复:

fn create_child1(root: LinkedList<Arc<i32>>) -> Vec<LinkedList<Arc<i32>>> {
    let data = root.back().expect("can't get tail.");
    let count = **data;
    if count > 0 {
        let mut rs = vec![];
        for i in 0..count {
            let mut l = LinkedList::new();
            root.iter().for_each(|a| {
                l.push_back(a.clone());
            });
            l.push_back(Arc::new(count - 1));
            let ref mut vec1 = create_child1(l);
            rs.append(vec1);
        }
        rs
    } else {
        vec![root]
    }
}

这个就能正常来着

phper-chen 2020-03-13 22:36

嗯 新的有出栈返回值了,旧版的是不是要解开一下再返回,我也是看着玩😂 对以下内容的回复:

fn create_child1(root: LinkedList<Arc<i32>>) -> Vec<LinkedList<Arc<i32>>> {
    let data = root.back().expect("can't get tail.");
    let count = **data;
    if count > 0 {
        let mut rs = vec![];
        for i in 0..count {
            let mut l = LinkedList::new();
            root.iter().for_each(|a| {
                l.push_back(a.clone());
            });
            l.push_back(Arc::new(count - 1));
            let ref mut vec1 = create_child1(l);
            rs.append(vec1);
        }
        rs
    } else {
        vec![root]
    }
}

这个就能正常来着

作者 LayneYy 2020-03-13 19:40

对以下内容的回复:

fn create_child1(root: LinkedList<Arc<i32>>) -> Vec<LinkedList<Arc<i32>>> {
    let data = root.back().expect("can't get tail.");
    let count = **data;
    if count > 0 {
        let mut rs = vec![];
        for i in 0..count {
            let mut l = LinkedList::new();
            root.iter().for_each(|a| {
                l.push_back(a.clone());
            });
            l.push_back(Arc::new(count - 1));
            let ref mut vec1 = create_child1(l);
            rs.append(vec1);
        }
        rs
    } else {
        vec![root]
    }
}

这个就能正常来着

作者 LayneYy 2020-03-13 17:51

应该没拿呀,里面的数据都是Arc.clone()出来的,原本的数据应该没动,这是动手写的第一个例子,就要翻车了。 对以下内容的回复:

1 共 4 条评论, 1 页