use super::{super::problem::*, receiver::*};

use std::{cell::*, sync::*};

//
// ProblemReceiverRef
//

/// Common reference type for [ProblemReceiver].
pub type ProblemReceiverRef<'receiver> = Arc<RefCell<&'receiver mut dyn ProblemReceiver>>;

impl<'receiver> ProblemReceiver for ProblemReceiverRef<'receiver> {
    fn give(&mut self, problem: Problem) -> Result<(), Problem> {
        self.borrow_mut().give(problem)
    }
}

//
// ProblemReceiverAsRef
//

/// As problem receiver reference.
pub trait ProblemReceiverAsRef<'this, ProblemReceiverT> {
    /// As problem receiver reference.
    fn as_ref(&'this mut self) -> ProblemReceiverRef<'this>;
}

impl<'this, ProblemReceiverT> ProblemReceiverAsRef<'this, ProblemReceiverT> for ProblemReceiverT
where
    ProblemReceiverT: ProblemReceiver,
{
    fn as_ref(&'this mut self) -> ProblemReceiverRef<'this> {
        ProblemReceiverRef::new(RefCell::new(self))
    }
}
