Code Monkey home page Code Monkey logo

clean-rust's People

Watchers

 avatar

clean-rust's Issues

Decouple non-determinism using trait bounds

Examples of non-determinism:

  • Randomness sources
  • Clocks

They can be extracted to a trait and injected using trait bounds and PhantomData on injected structs. This is the equivalent of implicit parameters in Scala.

Cargo clippy check for two merging two consecutive .map().map()

https://github.com/andreisilviudragnea/problems-rust/commit/e8ce9061270b5c73e5de3db5366aabb742c8a719

        let nodes = s[1..s.len() - 1]
            .split(',')
            .map(|str| match str {
                "null" => None,
                v => Some(v.parse::<i32>().unwrap()),
            })
            .map(|it| {
                it.map(|val| {
                    Rc::new(RefCell::new(TreeNode {
                        val,
                        left: None,
                        right: None,
                    }))
                })
            })
            .collect::<Vec<_>>();
        let nodes = s[1..s.len() - 1]
            .split(',')
            .map(|str| match str {
                "null" => None,
                v => Some(Rc::new(RefCell::new(TreeNode {
                    val: v.parse::<i32>().unwrap(),
                    left: None,
                    right: None,
                }))),
            })
            .collect::<Vec<_>>();

Less clone can lead to more code coupling - NOT true

Global config struct needs to be shared between application components between an Arc<T>, even if only one field is needed, in order to avoid multiple Arc<T> on each subfield.

Update:
This is not true because you can move struct fields to separate Arc<T> as needed.

Cargo clippy: replace .rev().collect::<Vec<_>>() and .pop() with .next()

https://github.com/andreisilviudragnea/problems-rust/commit/5a10d8e0e53486fc34073a3f4c898ade9f1ba85d

    pub(crate) fn from_str(s: &str) -> Option<Rc<RefCell<TreeNode>>> {
        if s == "[]" {
            return None;
        }

        let nodes = s[1..s.len() - 1]
            .split(',')
            .map(|str| match str {
                "null" => None,
                v => Some(Rc::new(RefCell::new(TreeNode {
                    val: v.parse::<i32>().unwrap(),
                    left: None,
                    right: None,
                }))),
            })
            .collect::<Vec<_>>();

        let mut kids = nodes.iter().cloned().rev().collect::<Vec<_>>();

        let root = kids.pop().unwrap();

        for node in nodes.into_iter().flatten() {
            if let Some(val) = kids.pop() {
                node.borrow_mut().left = val;
            }
            if let Some(val) = kids.pop() {
                node.borrow_mut().right = val;
            }
        }

        root
    }
pub(crate) fn from_str(s: &str) -> Option<Rc<RefCell<TreeNode>>> {
    if s == "[]" {
        return None;
    }

    let nodes = s[1..s.len() - 1]
        .split(',')
        .map(|str| match str {
            "null" => None,
            v => Some(Rc::new(RefCell::new(TreeNode {
                val: v.parse::<i32>().unwrap(),
                left: None,
                right: None,
            }))),
        })
        .collect::<Vec<_>>();

    let mut kids = nodes.iter().cloned();

    let root = kids.next().unwrap();

    for node in nodes.iter().flatten() {
        if let Some(val) = kids.next() {
            node.borrow_mut().left = val;
        }
        if let Some(val) = kids.next() {
            node.borrow_mut().right = val;
        }
    }

    root
}

Cargo clippy check for collect().iter() patterns

https://github.com/andreisilviudragnea/problems-rust/commit/a587070a62bdb5d94c32ec7fe0bad3c0805104e3
https://github.com/andreisilviudragnea/problems-rust/commit/bc425bb33048a9f99eca6cd12b05821fa5f804c5

        let nodes = s[1..s.len() - 1]
            .split(',')
            .map(|str| match str {
                "null" => None,
                v => Some(v.parse::<i32>().unwrap()),
            })
            .collect::<Vec<_>>() // THIS CAN
            .iter() // BE REMOVED
            .map(|it| {
                it.map(|val| {
                    Rc::new(RefCell::new(TreeNode {
                        val,
                        left: None,
                        right: None,
                    }))
                })
            })
            .collect::<Vec<_>>();

Do not remove .collect() calls if the collectors are used more than once.

RwLock deadlock

Lock read then write on the same thread results in deadlock

Removing .clone() calls can lead to cleaner code

Because it forces you to think about data ownership.

Strategy:

  • First remove #derive[(Clone)] from as many types as possible. This usually results in many Arcs on which .clone is called instead.
  • Then check if those Arcs are really needed by looking if .clone() on those Arcs is really needed.

Use Cargo workspace dependencies

In Cargo workspaces, specify dependencies only in root Cargo.toml and refer them in other files to reduce duplication and centralize versions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.