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

Namaste

Copyright (C) 2019, 2021-2025  Anonymous

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

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/>.

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

#![cfg(windows)]
#![doc(cfg(windows))]

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

macro_rules! read { ($self: ident) => {{
    let read = Namaste::make(&$self.read_id, Instance::Many)?;
    match $self.make_write() {
        Ok(_) => Ok(read),
        Err(_) => Err(err!("Another write is working")),
    }
}}}

macro_rules! write { ($self: ident) => {{
    let write = $self.make_write()?;
    match Namaste::make(&$self.read_id, Instance::One) {
        Ok(_) => Ok(write),
        Err(_) => Err(err!("Another read is working")),
    }
}}}

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
    #[cfg(not(feature="tokio"))]
    #[doc(cfg(not(feature="tokio")))]
    pub (crate) fn read(&self) -> Result<Namaste> {
        read!(self)
    }

    /// # Takes a read
    #[cfg(feature="tokio")]
    #[doc(cfg(feature="tokio"))]
    pub (crate) async fn read(&self) -> Result<Namaste> {
        read!(self)
    }

    /// # Takes a write
    #[cfg(not(feature="tokio"))]
    #[doc(cfg(not(feature="tokio")))]
    pub (crate) fn write(&self) -> Result<Namaste> {
        write!(self)
    }

    /// # Takes a write
    #[cfg(feature="tokio")]
    #[doc(cfg(feature="tokio"))]
    pub (crate) async fn write(&self) -> Result<Namaste> {
        write!(self)
    }

}
