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
//! Cross-platform compatibility utilities for consistent behavior across
//! Windows, macOS, Linux, and WebAssembly targets.
//!
//! This module provides helper functions that abstract over platform differences
//! so that the rest of the SciRS2 codebase can remain platform-agnostic.
//!
//! # Examples
//!
//! ```
//! use scirs2_core::platform_compat;
//!
//! // Portable temporary directory
//! let tmp = platform_compat::temp_dir();
//! assert!(tmp.is_absolute());
//!
//! // Portable temporary file path
//! let f = platform_compat::temp_file("my_data.bin");
//! assert!(f.ends_with("my_data.bin"));
//!
//! // CPU count
//! let n = platform_compat::num_cpus();
//! assert!(n >= 1);
//! ```
use std::path::PathBuf;
/// Return the platform's temporary directory.
///
/// On Unix this is typically `/tmp`; on Windows it is `%TEMP%` or similar.
/// Always prefer this over hard-coding `/tmp/`.
#[inline]
pub fn temp_dir() -> PathBuf {
std::env::temp_dir()
}
/// Build a [`PathBuf`] pointing to `<temp_dir>/<name>`.
///
/// Useful for constructing throwaway file paths in tests and transient
/// storage configurations.
#[inline]
pub fn temp_file(name: &str) -> PathBuf {
let mut p = std::env::temp_dir();
p.push(name);
p
}
/// Build a [`PathBuf`] pointing to `<temp_dir>/<subdir>/<name>`.
///
/// Creates a namespaced temporary path without actually creating the directory.
#[inline]
pub fn temp_path(subdir: &str, name: &str) -> PathBuf {
let mut p = std::env::temp_dir();
p.push(subdir);
p.push(name);
p
}
/// Return the number of logical CPUs available on the current machine.
///
/// Falls back to `1` if the value cannot be determined (e.g. under some
/// sandboxed or embedded environments).
#[inline]
pub fn num_cpus() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}
/// The native path separator character for the current platform.
///
/// `'/'` on Unix, `'\\'` on Windows.
#[inline]
pub fn path_separator() -> char {
std::path::MAIN_SEPARATOR
}
/// `true` when compiled for a Windows target.
#[inline]
pub const fn is_windows() -> bool {
cfg!(target_os = "windows")
}
/// `true` when compiled for a macOS target.
#[inline]
pub const fn is_macos() -> bool {
cfg!(target_os = "macos")
}
/// `true` when compiled for a Linux target.
#[inline]
pub const fn is_linux() -> bool {
cfg!(target_os = "linux")
}
/// `true` when compiled for a WebAssembly target.
#[inline]
pub const fn is_wasm() -> bool {
cfg!(target_family = "wasm")
}
/// `true` when compiled for any Unix-family target (Linux, macOS, BSDs, etc.).
#[inline]
pub const fn is_unix() -> bool {
cfg!(target_family = "unix")
}
/// Return the default temporary directory as a [`String`].
///
/// Convenience wrapper around [`temp_dir`] for code that stores paths as
/// `String` fields (e.g. configuration structs).
pub fn temp_dir_string() -> String {
temp_dir()
.to_str()
.unwrap_or(if cfg!(target_os = "windows") {
"C:\\Temp"
} else {
"/tmp"
})
.to_string()
}
/// Join a subdirectory name onto the platform temporary directory and return
/// the result as a [`String`].
pub fn temp_subdir_string(subdir: &str) -> String {
let mut p = temp_dir();
p.push(subdir);
p.to_str()
.unwrap_or(if cfg!(target_os = "windows") {
"C:\\Temp"
} else {
"/tmp"
})
.to_string()
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn temp_dir_is_absolute() {
assert!(temp_dir().is_absolute());
}
#[test]
fn temp_file_ends_with_name() {
let p = temp_file("hello.txt");
assert!(p.ends_with("hello.txt"));
}
#[test]
fn temp_path_contains_subdir() {
let p = temp_path("scirs2", "data.bin");
assert!(p.ends_with("data.bin"));
// The parent should contain "scirs2"
let parent = p.parent().expect("should have parent");
assert!(parent.ends_with("scirs2"));
}
#[test]
fn num_cpus_at_least_one() {
assert!(num_cpus() >= 1);
}
#[test]
fn path_separator_is_correct() {
let sep = path_separator();
if cfg!(target_os = "windows") {
assert_eq!(sep, '\\');
} else {
assert_eq!(sep, '/');
}
}
#[test]
fn platform_detection_consistent() {
// At least one platform family should be true
let any = is_windows() || is_unix() || is_wasm();
assert!(any, "should detect at least one platform family");
}
#[test]
fn temp_dir_string_is_nonempty() {
assert!(!temp_dir_string().is_empty());
}
#[test]
fn temp_subdir_string_contains_subdir() {
let s = temp_subdir_string("scirs2_test");
assert!(s.contains("scirs2_test"));
}
}