1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Namaste

Copyright (C) 2019, 2021-2024  Anonymous

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

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

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

//! # Namaste
//!
//! ## Project
//!
//! - License: GNU Lesser General Public License, either version 3, or (at your option) any later version.
//! - _This project follows [Semantic Versioning 2.0.0]_
//!
//! ## Features
//!
//! - Handling locks amongst processes.
//! - Some [extensions][trait:UdsxUnixStream] for Unix domain sockets.
//!
//! ## Locks
//!
//! ### Usage
//!
//! An identifier is a non-empty byte array. Up to 64 bytes are supported. So, for example, you can use SHA3-512 hashes as IDs.
//!
//! You can call [`make()`][fn:make] or [`make_wait()`][fn:make_wait] to lock your IDs. When done, simply drop them via `drop()`.
//!
//! ### Notes
//!
//! | System  | Requirements | Implementation details
//! | ------- | ------------ | ----------------------
//! | Linux   | Nightly Rust | Abstract sockets (see `unix(7)`)
//! | Windows |              | [Event Objects][win:event-objects]
//!
//! Other systems are not supported.
//!
//! [Semantic Versioning 2.0.0]: https://semver.org/spec/v2.0.0.html
//! [win:event-objects]: https://learn.microsoft.com/en-us/windows/win32/sync/using-event-objects
//!
//! [fn:make]: fn.make.html
//! [fn:make_wait]: fn.make_wait.html
//! [trait:UdsxUnixStream]: trait.UdsxUnixStream.html

// TODO: remove these when related APIs are stabilized
#![feature(doc_cfg)]
#![cfg_attr(any(target_os="linux", target_os="l4re", target_os="android"), feature(tcp_quickack))]
#![cfg_attr(any(target_os="linux", target_os="l4re", target_os="android"), feature(btree_extract_if))]
#![cfg_attr(target_family="unix", feature(unix_socket_ancillary_data))]

// ╔═════════════════╗
// ║   IDENTIFIERS   ║
// ╚═════════════════╝

macro_rules! code_name  { () => { "namaste" }}
macro_rules! version    { () => { "0.29.0" }}

/// # Crate name
pub const NAME: &str = "Namaste";

/// # Crate code name
pub const CODE_NAME: &str = code_name!();

/// # ID of this crate
pub const ID: &str = concat!(
    "b1b4db58-59414682-a029819c-84b9a4e1-170e0050-92722253-fae9dfd6-b652a960-",
    "e553656c-6b625519-392bb87b-c23a6af3-c498c567-ff5c242f-606d2694-9113a891",
);

/// # Crate version
pub const VERSION: &str = version!();

/// # Crate release date (year/month/day)
pub const RELEASE_DATE: (u16, u8, u8) = (2024, 4, 18);

/// # Tag, which can be used for logging...
pub const TAG: &str = concat!(code_name!(), "::b1b4db58::", version!());

// ╔════════════════════╗
// ║   IMPLEMENTATION   ║
// ╚════════════════════╝

extern crate alloc;

/// # Wrapper for format!(), which prefixes your optional message with: crate::TAG, module_path!(), line!()
macro_rules! __ {
    ($($arg: tt)+) => {
        format!("[{tag}][{module_path}-{line}] {msg}", tag=crate::TAG, module_path=module_path!(), line=line!(), msg=format!($($arg)+))
    };
    () => {
        format!("[{tag}][{module_path}-{line}] (internal error)", tag=crate::TAG, module_path=module_path!(), line=line!())
    };
}

/// # Makes new std::io::Error
macro_rules! err {
    ($kind: path, $($arg: tt)+) => { std::io::Error::new($kind, __!($($arg)+)) };
    ($($arg: tt)+) => { err!(std::io::ErrorKind::Other, $($arg)+) };
    () => { std::io::Error::new(std::io::ErrorKind::Other, __!()) };
}

#[cfg(not(windows))]
macro_rules! plural_s { ($n: expr) => { if $n == 1 { concat!() } else { concat!('s') }}}

macro_rules! async_call { ($f: expr) => {{
    #[cfg(feature="async-std")]
    let r = $f.await;
    #[cfg(not(feature="async-std"))]
    let r = $f;
    r
}}}

macro_rules! sleep_duration { () => { 10 }}

pub mod debts;

// ╔═════════════════════╗
// ║   Linux & Windows   ║
// ╚═════════════════════╝

#[cfg(any(target_os="android", target_os="l4re", target_os="linux", windows))]
#[doc(cfg(any(target_os="android", target_os="l4re", target_os="linux", windows)))]
mod root;

#[cfg(any(target_os="android", target_os="l4re", target_os="linux", windows))]
#[doc(cfg(any(target_os="android", target_os="l4re", target_os="linux", windows)))]
pub use self::root::*;

// ╔══════════╗
// ║   Unix   ║
// ╚══════════╝

#[cfg(unix)]
#[doc(cfg(unix))]
mod udsx_unix_stream;

#[cfg(unix)]
#[doc(cfg(unix))]
pub use self::udsx_unix_stream::*;

// ╔═══════════╗
// ║   Linux   ║
// ╚═══════════╝

#[cfg(any(target_os="android", target_os="l4re", target_os="linux"))]
#[doc(cfg(any(target_os="android", target_os="l4re", target_os="linux")))]
mod linux;

#[cfg(any(target_os="android", target_os="l4re", target_os="linux"))]
#[doc(cfg(any(target_os="android", target_os="l4re", target_os="linux")))]
pub use self::linux::Namaste;

// ╔═════════════╗
// ║   Windows   ║
// ╚═════════════╝

#[cfg(windows)]
#[doc(cfg(windows))]
mod windows;

#[cfg(windows)]
#[doc(cfg(windows))]
pub use self::windows::Namaste;

// ╔═════════╗
// ║   All   ║
// ╚═════════╝

pub mod version_info;

/// # Result type used in this crate
pub type Result<T> = std::io::Result<T>;

#[test]
fn test_crate_version() {
    assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
}