rlimit/windows.rs
1use std::io;
2use std::os::raw::c_int;
3
4extern "C" {
5 fn _setmaxstdio(new_max: c_int) -> c_int;
6 fn _getmaxstdio() -> c_int;
7}
8
9/// Sets a maximum for the number of simultaneously open files at the stream I/O level.
10///
11/// See <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmaxstdio?view=msvc-170>
12///
13/// # Errors
14/// See the official documentation
15#[cfg_attr(docsrs, doc(cfg(windows)))]
16pub fn setmaxstdio(new_max: u32) -> io::Result<u32> {
17 // A negative `new_max` will cause EINVAL.
18 // A negative `ret` should never appear.
19 // It is safe even if the return value is wrong.
20 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
21 unsafe {
22 let ret = _setmaxstdio(new_max as c_int);
23 if ret < 0 {
24 return Err(io::Error::last_os_error());
25 }
26 Ok(ret as u32)
27 }
28}
29
30/// Returns the number of simultaneously open files permitted at the stream I/O level.
31///
32/// See <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getmaxstdio?view=msvc-170>
33#[cfg_attr(docsrs, doc(cfg(windows)))]
34#[must_use]
35pub fn getmaxstdio() -> u32 {
36 // A negative `ret` should never appear.
37 // It is safe even if the return value is wrong.
38 #[allow(clippy::cast_sign_loss)]
39 unsafe {
40 let ret = _getmaxstdio();
41 debug_assert!(ret >= 0);
42 ret as u32
43 }
44}