pub mod dylib;
pub use dylib::{DylibError, DynamicLibrary, Symbol, build_dylib_name, get_dylib_extension, get_dylib_prefix};
#[cfg(target_os = "windows")]
pub const fn is_windows() -> bool {
true
}
#[cfg(not(target_os = "windows"))]
pub const fn is_windows() -> bool {
false
}
#[cfg(target_os = "macos")]
pub const fn is_macos() -> bool {
true
}
#[cfg(not(target_os = "macos"))]
pub const fn is_macos() -> bool {
false
}
#[cfg(target_os = "linux")]
pub const fn is_linux() -> bool {
true
}
#[cfg(not(target_os = "linux"))]
pub const fn is_linux() -> bool {
false
}
#[cfg(target_os = "freebsd")]
pub const fn is_freebsd() -> bool {
true
}
#[cfg(not(target_os = "freebsd"))]
pub const fn is_freebsd() -> bool {
false
}
#[cfg(target_os = "netbsd")]
pub const fn is_netbsd() -> bool {
true
}
#[cfg(not(target_os = "netbsd"))]
pub const fn is_netbsd() -> bool {
false
}
pub const fn is_unix() -> bool {
!is_windows()
}
pub const fn is_supported_platform() -> bool {
is_windows() || is_linux() || is_macos()
}
#[cfg(target_pointer_width = "64")]
pub type PlatformPointer = u64;
#[cfg(target_pointer_width = "32")]
pub type PlatformPointer = u32;
#[cfg(target_pointer_width = "64")]
pub type PlatformSize = u64;
#[cfg(target_pointer_width = "32")]
pub type PlatformSize = u32;
pub const fn align_size(size: usize, alignment: usize) -> usize {
let mask = alignment - 1;
(size + mask) & !mask
}
pub fn is_aligned(ptr: *const u8, alignment: usize) -> bool {
let addr = ptr as usize;
addr % alignment == 0
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endianness {
Little,
Big,
}
#[derive(Debug, Clone)]
pub struct PlatformInfo {
pub name: String,
pub pointer_size: usize,
pub endianness: Endianness,
pub os_type: OsType,
pub arch: Arch,
}
impl PlatformInfo {
pub fn current() -> Self {
Self {
name: Self::get_platform_name(),
pointer_size: std::mem::size_of::<*const u8>(),
endianness: Self::get_endianness(),
os_type: Self::get_os_type(),
arch: Self::get_arch(),
}
}
fn get_platform_name() -> String {
#[cfg(target_os = "windows")]
{
"Windows".to_string()
}
#[cfg(target_os = "macos")]
{
"macOS".to_string()
}
#[cfg(target_os = "linux")]
{
"Linux".to_string()
}
#[cfg(target_os = "freebsd")]
{
"FreeBSD".to_string()
}
#[cfg(target_os = "netbsd")]
{
"NetBSD".to_string()
}
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd"
)))]
{
"Unknown".to_string()
}
}
fn get_endianness() -> Endianness {
#[cfg(target_endian = "little")]
{
Endianness::Little
}
#[cfg(target_endian = "big")]
{
Endianness::Big
}
}
fn get_os_type() -> OsType {
#[cfg(target_os = "windows")]
{
OsType::Windows
}
#[cfg(target_os = "macos")]
{
OsType::Macos
}
#[cfg(target_os = "linux")]
{
OsType::Linux
}
#[cfg(target_os = "freebsd")]
{
OsType::Freebsd
}
#[cfg(target_os = "netbsd")]
{
OsType::Netbsd
}
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd"
)))]
{
OsType::Unknown
}
}
fn get_arch() -> Arch {
#[cfg(target_arch = "x86_64")]
{
Arch::X86_64
}
#[cfg(target_arch = "x86")]
{
Arch::X86
}
#[cfg(target_arch = "aarch64")]
{
Arch::Aarch64
}
#[cfg(target_arch = "arm")]
{
Arch::Arm
}
#[cfg(target_arch = "riscv64")]
{
Arch::Riscv64
}
#[cfg(target_arch = "riscv32")]
{
Arch::Riscv32
}
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32"
)))]
{
Arch::Unknown
}
}
pub fn is_64bit(&self) -> bool {
self.pointer_size == 8
}
pub fn is_little_endian(&self) -> bool {
self.endianness == Endianness::Little
}
pub fn is_big_endian(&self) -> bool {
self.endianness == Endianness::Big
}
pub fn is_x86_64(&self) -> bool {
self.arch == Arch::X86_64
}
pub fn is_x86(&self) -> bool {
self.arch == Arch::X86
}
pub fn is_aarch64(&self) -> bool {
self.arch == Arch::Aarch64
}
pub fn is_arm(&self) -> bool {
self.arch == Arch::Arm
}
pub fn is_riscv(&self) -> bool {
self.arch == Arch::Riscv64 || self.arch == Arch::Riscv32
}
pub fn is_32bit(&self) -> bool {
self.pointer_size == 4
}
pub fn to_string(&self) -> String {
format!("{} ({:?}, {}bit, {:?})", self.name, self.arch, if self.is_64bit() { 64 } else { 32 }, self.endianness)
}
}
impl Default for PlatformInfo {
fn default() -> Self {
Self::current()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsType {
Windows,
Macos,
Linux,
Freebsd,
Netbsd,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
X86_64,
X86,
Aarch64,
Arm,
Riscv64,
Riscv32,
Unknown,
}