Giter VIP home page Giter VIP logo

Comments (3)

orium avatar orium commented on July 23, 2024

Can you give me a complete program? That one is missing some definitions. (I changed some stuff to make it compile but the test ran without a problem.)

from rpds.

candronikos avatar candronikos commented on July 23, 2024

Code at end of comment. I only want to implement PartialEq so I can perform unit tests for equality tests. Now that they're all in the one file, I'm getting a new error (maybe because of the double impl for the HVal trait and the HDict trait?)

What I find strange is that originally, the first two commented out asserts worked.

In my main code where these components are split into other modules:

New Error

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)

Code

extern crate rpds;

use std::fmt::Debug;
use std::any::{Any,TypeId};

use rpds::HashTrieMap;
use rpds::map::hash_trie_map::Iter;

pub trait HVal: Any + Debug {
    fn to_zinc(&self) -> String;

    fn to_string(&self) -> String {
        self.to_zinc()
    }

    fn _get_type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }
}

///*
impl Eq for HVal {}

impl PartialEq for HVal {
    fn eq(&self, other: &Self) -> bool {
        //self.tr_eq(other)
        match self._get_type_id() == other._get_type_id() {
            true => self == other,
            false => false
        }
    }
}
//*/

pub type HBool = bool;

impl HVal for HBool {
    fn to_zinc(&self) -> String {
        match *self {
            true => "T".to_string(),
            false => "F".to_string(),
        }
    }
}

type ContainedHval = Box<HVal>;

#[derive(Clone,Debug)]
pub struct HDict(HashTrieMap<String, ContainedHval>);

impl HVal for HDict {
    fn to_zinc(&self) -> String {
        "Not implemented!".to_string()
    }
}

///*
impl Eq for HDict {}

impl PartialEq for HDict {
    fn eq(&self, other: &Self) -> bool {
        if self.0.size() != other.0.size() {
            return false;
        }

        for (k,v) in self.0.iter() {
            let tmp = match other.0.get(k) {
                Some(ov) => **v == **ov,
                None => false
            };

            if !tmp {
                return false;
            }
        }

        true
    }
}
//*/

impl HDict {
    pub fn new() -> HDict {
        HDict(HashTrieMap::new())
    }

    fn has(&self, name: &String) -> HBool {
        self.0.contains_key(name)
    }

    fn is_empty(&self) -> HBool {
        self.0.is_empty()
    }

    fn missing(&self, name: &String) -> HBool {
        !self.0.contains_key(name)
    }

    pub fn iter(&self) -> Iter<String, ContainedHval> {
        self.0.iter()
    }

    fn insert(&mut self, name: &String, value: ContainedHval) -> Self {
        HDict(self.0.insert(name.clone(),value))
    }
}

fn main() {
    let a = HDict::new();
    let b = HDict::new().insert(&"Hello".to_owned(),Box::new(true));

    //assert_ne!(a,a.clone().insert(&"Hello".to_owned(),Box::new(true)));
    //assert_eq!(a,HDict::new());
    assert_eq!(
        b.clone(),
        HDict::new().insert(&"Hello".to_owned(),Box::new(true))
    );
}

from rpds.

orium avatar orium commented on July 23, 2024

That's because HVal::eq() calls itself endlessly:

impl PartialEq for HVal {
    fn eq(&self, other: &Self) -> bool {
        match self._get_type_id() == other._get_type_id() {
            true => self == other,             // !!! This is a recursive call
            false => false
        }
    }
}

from rpds.

Related Issues (20)

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.