fn calc_state(&self, previous_state: & State, event: & Event, event_line: usize, hash: &u64) -> State {
        /* a Variable cannot borrow or return resource from Functions, 
        but can 'lend' or 'reaquire' to Functions (pass itself by reference and take it back); */
        fn event_invalid(event: & Event) -> bool {
            match event {
                Event::StaticBorrow{ from: ResourceAccessPoint::Function(_) } => true,
                Event::MutableBorrow{ from: ResourceAccessPoint::Function(_) } => true,
                Event::StaticReturn{ to: Some(ResourceAccessPoint::Function(_)) } => true,
                Event::MutableReturn{ to: Some(ResourceAccessPoint::Function(_)) } => true,
                _ => false,
            }
        }
        if event_invalid(event) { return State::Invalid; }

        match (previous_state, event) {
            (State::Invalid, _) => State::Invalid,

            (State::OutOfScope, Event::Acquire{ from: _ })  => State::FullPrivilege,

            (State::OutOfScope, Event::StaticBorrow{ from: ro })  =>
                State::PartialPrivilege {
                    borrow_count: 1,
                    borrow_to: [ro.to_owned()].iter().cloned().collect()
                },

            (State::OutOfScope, Event::MutableBorrow{ from: _ }) =>
                State::FullPrivilege,

            (State::FullPrivilege, Event::Move{to: to_ro}) => State::ResourceMoved{ move_to: to_ro.to_owned(), move_at_line: event_line },

            (State::FullPrivilege, Event::MutableLend{ to: to_ro }) =>
                if self.is_mut(hash) { State::RevokedPrivilege{ to: None, borrow_to: to_ro.to_owned() } } else { State::Invalid },
            
            // happends when a mutable reference returns, invalid otherwise
            (State::FullPrivilege, Event::MutableReturn{ to: _ }) =>
                State::OutOfScope,
        
            (State::FullPrivilege, Event::OwnerGoOutOfScope) => State::OutOfScope,

            (State::FullPrivilege, Event::RefGoOutOfScope) => State::OutOfScope,

            // this looks impossible?
            // (State::FullPrivilege, Event::MutableReacquire{ from: ro }) =>
            //     State::ResourceReturned { to: ro.to_owned() },

            // (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::MutableReacquire{ from: ro }) =>
            //         State::ResourceReturned { to: ro.to_owned() },

            (State::FullPrivilege, Event::StaticLend{ to: to_ro }) =>
                State::PartialPrivilege {
                    borrow_count: 1,
                    borrow_to: [(to_ro.to_owned().unwrap())].iter().cloned().collect() // we assume there is no borrow_to:None
                },

            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::MutableLend{ to: _ }) => State::Invalid,

            (State::PartialPrivilege{ borrow_count: current, borrow_to }, Event::StaticLend{ to: to_ro }) => {
                let mut new_borrow_to = borrow_to.clone();
                // Assume can not lend to None
                new_borrow_to.insert(to_ro.to_owned().unwrap());
                State::PartialPrivilege {
                    borrow_count: current+1,
                    borrow_to: new_borrow_to,
                }
            }
                

            // self statically borrowed resource, and it returns; TODO what about references to self?
            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::StaticReturn{ to: _ }) => State::OutOfScope,

            (State::PartialPrivilege{ borrow_count, borrow_to }, Event::StaticReacquire{ from: ro }) => {
                let new_borrow_count = borrow_count - 1;
                // check if it resumes to full privilege    
                if borrow_count - 1 == 0 {
                        State::FullPrivilege 
                    } else {
                        let mut new_borrow_to = borrow_to.clone();
                        // TODO ro.unwrap() should not panic, because Reacquire{from: None} is not possible
                        // TODO change to Reaquire{from: ResourceAccessPoint}
                        assert_eq!(new_borrow_to.remove(&ro.to_owned().unwrap()), true); // borrow_to must contain ro
                        State::PartialPrivilege{
                            borrow_count: new_borrow_count,
                            borrow_to: new_borrow_to,
                        }
                    }
                }

            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::OwnerGoOutOfScope) => State::OutOfScope,

            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::RefGoOutOfScope) => State::OutOfScope,

            (State::RevokedPrivilege{ to: _, borrow_to: _ }, Event::MutableReacquire{ from: _ }) => State::FullPrivilege,

            (_, Event::Duplicate { to: _ }) =>
                (*previous_state).clone(),

             // State::FullPrivilege,

            (_, _) => State::Invalid,
        }
    }
// a Function does not have a State, so we assume previous_state is always for Variables
a Function does not have a State, so we assume previous_state is always for Variables
a Function does not have a State so we assume previous_state is always for Variables
fn calc_state(&self, previous_state: & State, event: & Event, event_line: usize, hash: &u64) -> State {
        /* a Variable cannot borrow or return resource from Functions, 
        but can 'lend' or 'reaquire' to Functions (pass itself by reference and take it back); */
        fn event_invalid(event: & Event) -> bool {
            match event {
                Event::StaticBorrow{ from: ResourceAccessPoint::Function(_) } => true,
                Event::MutableBorrow{ from: ResourceAccessPoint::Function(_) } => true,
                Event::StaticReturn{ to: Some(ResourceAccessPoint::Function(_)) } => true,
                Event::MutableReturn{ to: Some(ResourceAccessPoint::Function(_)) } => true,
                _ => false,
            }
        }
        if event_invalid(event) { return State::Invalid; }
        match (previous_state, event) {
            (State::Invalid, _) => State::Invalid,
            (State::OutOfScope, Event::Acquire{ from: _ })  => State::FullPrivilege,
            (State::OutOfScope, Event::StaticBorrow{ from: ro })  =>
                State::PartialPrivilege {
                    borrow_count: 1,
                    borrow_to: [ro.to_owned()].iter().cloned().collect()
                },
            (State::OutOfScope, Event::MutableBorrow{ from: _ }) =>
                State::FullPrivilege,
            (State::FullPrivilege, Event::Move{to: to_ro}) => State::ResourceMoved{ move_to: to_ro.to_owned(), move_at_line: event_line },
            (State::FullPrivilege, Event::MutableLend{ to: to_ro }) =>
                if self.is_mut(hash) { State::RevokedPrivilege{ to: None, borrow_to: to_ro.to_owned() } } else { State::Invalid },
            // happends when a mutable reference returns, invalid otherwise
            (State::FullPrivilege, Event::MutableReturn{ to: _ }) =>
                State::OutOfScope,
            (State::FullPrivilege, Event::OwnerGoOutOfScope) => State::OutOfScope,
            (State::FullPrivilege, Event::RefGoOutOfScope) => State::OutOfScope,
            // this looks impossible?
            // (State::FullPrivilege, Event::MutableReacquire{ from: ro }) =>
            //     State::ResourceReturned { to: ro.to_owned() },
            // (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::MutableReacquire{ from: ro }) =>
            //         State::ResourceReturned { to: ro.to_owned() },
            (State::FullPrivilege, Event::StaticLend{ to: to_ro }) =>
                State::PartialPrivilege {
                    borrow_count: 1,
                    borrow_to: [(to_ro.to_owned().unwrap())].iter().cloned().collect() // we assume there is no borrow_to:None
                },
            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::MutableLend{ to: _ }) => State::Invalid,
            (State::PartialPrivilege{ borrow_count: current, borrow_to }, Event::StaticLend{ to: to_ro }) => {
                let mut new_borrow_to = borrow_to.clone();
                // Assume can not lend to None
                new_borrow_to.insert(to_ro.to_owned().unwrap());
                State::PartialPrivilege {
                    borrow_count: current+1,
                    borrow_to: new_borrow_to,
                }
            }
            // self statically borrowed resource, and it returns; TODO what about references to self?
            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::StaticReturn{ to: _ }) => State::OutOfScope,
            (State::PartialPrivilege{ borrow_count, borrow_to }, Event::StaticReacquire{ from: ro }) => {
                let new_borrow_count = borrow_count - 1;
                // check if it resumes to full privilege    
                if borrow_count - 1 == 0 {
                        State::FullPrivilege 
                    } else {
                        let mut new_borrow_to = borrow_to.clone();
                        // TODO ro.unwrap() should not panic, because Reacquire{from: None} is not possible
                        // TODO change to Reaquire{from: ResourceAccessPoint}
                        assert_eq!(new_borrow_to.remove(&ro.to_owned().unwrap()), true); // borrow_to must contain ro
                        State::PartialPrivilege{
                            borrow_count: new_borrow_count,
                            borrow_to: new_borrow_to,
                        }
                    }
                }
            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::OwnerGoOutOfScope) => State::OutOfScope,
            (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::RefGoOutOfScope) => State::OutOfScope,
            (State::RevokedPrivilege{ to: _, borrow_to: _ }, Event::MutableReacquire{ from: _ }) => State::FullPrivilege,
            (_, Event::Duplicate { to: _ }) =>
                (*previous_state).clone(),
             // State::FullPrivilege,
            (_, _) => State::Invalid,
        }
    }
fn calc_state ( & self , previous_state : & State , event : & Event , event_line : usize , hash : & u64 ) -> State { /* a Variable cannot borrow or return resource from Functions, 
        but can 'lend' or 'reaquire' to Functions (pass itself by reference and take it back); */ fn event_invalid ( event : & Event ) -> bool { match event { Event :: StaticBorrow { from : ResourceAccessPoint :: Function ( _ ) } => true , Event :: MutableBorrow { from : ResourceAccessPoint :: Function ( _ ) } => true , Event :: StaticReturn { to : Some ( ResourceAccessPoint :: Function ( _ ) ) } => true , Event :: MutableReturn { to : Some ( ResourceAccessPoint :: Function ( _ ) ) } => true , _ => false , } } if event_invalid ( event ) { return State :: Invalid ; } match ( previous_state , event ) { ( State :: Invalid , _ ) => State :: Invalid , ( State :: OutOfScope , Event :: Acquire { from : _ } ) => State :: FullPrivilege , ( State :: OutOfScope , Event :: StaticBorrow { from : ro } ) => State :: PartialPrivilege { borrow_count : 1 , borrow_to : [ ro . to_owned ( ) ] . iter ( ) . cloned ( ) . collect ( ) } , ( State :: OutOfScope , Event :: MutableBorrow { from : _ } ) => State :: FullPrivilege , ( State :: FullPrivilege , Event :: Move { to : to_ro } ) => State :: ResourceMoved { move_to : to_ro . to_owned ( ) , move_at_line : event_line } , ( State :: FullPrivilege , Event :: MutableLend { to : to_ro } ) => if self . is_mut ( hash ) { State :: RevokedPrivilege { to : None , borrow_to : to_ro . to_owned ( ) } } else { State :: Invalid } , // happends when a mutable reference returns, invalid otherwise ( State :: FullPrivilege , Event :: MutableReturn { to : _ } ) => State :: OutOfScope , ( State :: FullPrivilege , Event :: OwnerGoOutOfScope ) => State :: OutOfScope , ( State :: FullPrivilege , Event :: RefGoOutOfScope ) => State :: OutOfScope , // this looks impossible? // (State::FullPrivilege, Event::MutableReacquire{ from: ro }) => //     State::ResourceReturned { to: ro.to_owned() }, // (State::PartialPrivilege{ borrow_count: _, borrow_to: _ }, Event::MutableReacquire{ from: ro }) => //         State::ResourceReturned { to: ro.to_owned() }, ( State :: FullPrivilege , Event :: StaticLend { to : to_ro } ) => State :: PartialPrivilege { borrow_count : 1 , borrow_to : [ ( to_ro . to_owned ( ) . unwrap ( ) ) ] . iter ( ) . cloned ( ) . collect ( ) // we assume there is no borrow_to:None } , ( State :: PartialPrivilege { borrow_count : _ , borrow_to : _ } , Event :: MutableLend { to : _ } ) => State :: Invalid , ( State :: PartialPrivilege { borrow_count : current , borrow_to } , Event :: StaticLend { to : to_ro } ) => { let mut new_borrow_to = borrow_to . clone ( ) ; // Assume can not lend to None new_borrow_to . insert ( to_ro . to_owned ( ) . unwrap ( ) ) ; State :: PartialPrivilege { borrow_count : current + 1 , borrow_to : new_borrow_to , } } // self statically borrowed resource, and it returns; TODO what about references to self? ( State :: PartialPrivilege { borrow_count : _ , borrow_to : _ } , Event :: StaticReturn { to : _ } ) => State :: OutOfScope , ( State :: PartialPrivilege { borrow_count , borrow_to } , Event :: StaticReacquire { from : ro } ) => { let new_borrow_count = borrow_count - 1 ; // check if it resumes to full privilege     if borrow_count - 1 == 0 { State :: FullPrivilege } else { let mut new_borrow_to = borrow_to . clone ( ) ; // TODO ro.unwrap() should not panic, because Reacquire{from: None} is not possible // TODO change to Reaquire{from: ResourceAccessPoint} assert_eq ! ( new_borrow_to remove ( ro to_owned ( ) unwrap ( ) ) true ) ; // borrow_to must contain ro State :: PartialPrivilege { borrow_count : new_borrow_count , borrow_to : new_borrow_to , } } } ( State :: PartialPrivilege { borrow_count : _ , borrow_to : _ } , Event :: OwnerGoOutOfScope ) => State :: OutOfScope , ( State :: PartialPrivilege { borrow_count : _ , borrow_to : _ } , Event :: RefGoOutOfScope ) => State :: OutOfScope , ( State :: RevokedPrivilege { to : _ , borrow_to : _ } , Event :: MutableReacquire { from : _ } ) => State :: FullPrivilege , ( _ , Event :: Duplicate { to : _ } ) => ( * previous_state ) . clone ( ) , // State::FullPrivilege, ( _ , _ ) => State :: Invalid , } }
a Function does not have a State, so we assume previous_state is always for Variables
a Function does not have a State so we assume previous_state is always for Variables
&self
previous_state
& State
event
& Event
event_line
usize
hash
&u64