Documentation
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Namaste

Copyright (C) 2019, 2021-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2021-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

use {
    crate::{
        Result,
        windows::Instance,
    },
    super::{Namaste, RwNamaste},
};

impl<R, W> RwNamaste<R, W> where R: AsRef<[u8]>, W: AsRef<[u8]> {

    /// # Makes write
    fn make_write(&self) -> Result<Namaste> {
        Namaste::make(&self.write_id, Instance::One)
    }

    /// # Takes a read
    pub fn read(&mut self) -> Result<()> {
        match &self.read {
            None => {
                let read = Namaste::make(&self.read_id, Instance::Many)?;
                match self.make_write() {
                    Ok(_) => Ok(self.read = Some(read)),
                    Err(_) => Err(err!("Another write is working")),
                }
            },
            Some(_) => Ok(()),
        }
    }

    /// # Takes a write
    pub fn write(&mut self) -> Result<()> {
        match &self.write {
            None => {
                let write = self.make_write()?;
                match Namaste::make(&self.read_id, Instance::One) {
                    Ok(_) => Ok(self.write = Some(write)),
                    Err(_) => Err(err!("Another read is working")),
                }
            },
            Some(_) => Ok(()),
        }
    }

}