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
use core::fmt;

/// ## WARNING
///
/// This **will** leak memory without careful planning, as it does not run any destructors!
/// You need to make sure that all scopes end before calling this!
/// You can use
///
/// ```rust
/// fn main() {
///     {
///         // Main code
///         let a = vec![5];
///     }
///     ndless::process::abort();
/// }
/// ```
/// to ensure that no memory leaks.
pub fn abort() -> ! {
	unsafe { ndless_sys::abort() }
}

/// ## WARNING
///
/// This **will** leak memory without careful planning, as it does not run any destructors!
/// You need to make sure that all scopes end before calling this!
/// You can either use
///
/// ```rust
/// fn main() {
///     {
///         // Main code
///         let a = vec![5];
///     }
///     ndless::process::exit(1);
/// }
/// ```
/// or
/// ```rust
/// fn main() {
///     ndless::process::exit({
///         // Main code
///	        let a = vec![5];
///         0
///     });
/// }
/// ```
/// to ensure that no memory leaks.
pub fn exit(code: i32) -> ! {
	unsafe { ndless_sys::exit(code) }
}

/// A trait for implementing arbitrary return types in the `main` function.
///
/// The c-main function only supports to return integers as return type.
/// So, every type implementing the `Termination` trait has to be converted
/// to an integer.
///
/// The default implementations are returning 0 to indicate
/// a successful execution. In case of a failure, 1 is returned.
pub trait Termination {
	/// Is called to get the representation of the value as status code.
	/// This status code is returned to the operating system.
	fn report(self) -> i32;
}

impl Termination for () {
	#[inline]
	fn report(self) -> i32 {
		0
	}
}

impl Termination for i32 {
	#[inline]
	fn report(self) -> i32 {
		self
	}
}

impl<E: fmt::Debug> Termination for Result<(), E> {
	fn report(self) -> i32 {
		match self {
			Ok(()) => ().report(),
			Err(err) => Err::<!, _>(err).report(),
		}
	}
}

impl Termination for ! {
	fn report(self) -> i32 {
		self
	}
}

impl<E: fmt::Debug> Termination for Result<!, E> {
	fn report(self) -> i32 {
		let err = match self {
			Err(err) => err,
			_ => unreachable!(),
		};
		crate::msg::msg("Error", &alloc::format!("Error: {:?}", err));
		1
	}
}