Add struct TaskConfig

This commit is contained in:
Alex Kotov 2021-12-05 20:33:48 +05:00
parent 9263caa1c2
commit 4340397809
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 19 additions and 4 deletions

View file

@ -5,7 +5,8 @@ use crate::task::*;
use std::ffi::CString;
fn main() {
let wm_task = WMTask::start().unwrap();
let wm_task_config = TaskConfig::new("polytreewm");
let wm_task = WMTask::start(wm_task_config).unwrap();
unsafe { libc::exit(wm_task.wait().status()) }
}
@ -14,7 +15,7 @@ struct WMTask {
}
impl Task for WMTask {
fn start() -> Result<Self, String> {
fn start(config: TaskConfig) -> Result<Self, String> {
unsafe {
let pid = libc::fork();
@ -23,7 +24,7 @@ impl Task for WMTask {
}
if pid == 0 {
let arg0 = CString::new(b"polytreewm" as &[u8]).unwrap();
let arg0 = CString::new(config.exe().as_bytes()).unwrap();
let args = vec![arg0.as_ptr(), std::ptr::null()];
libc::execvp(arg0.as_ptr(), args.as_ptr());
libc::exit(libc::EXIT_FAILURE);

View file

@ -1,12 +1,26 @@
pub trait Task: Sized {
fn start() -> Result<Self, String>;
fn start(config: TaskConfig) -> Result<Self, String>;
fn wait(self) -> TaskResult;
}
pub struct TaskConfig {
exe: String,
}
pub struct TaskResult {
status: i32,
}
impl TaskConfig {
pub fn new<Exe: Into<String>>(exe: Exe) -> Self {
Self { exe: exe.into() }
}
pub fn exe(&self) -> &String {
&self.exe
}
}
impl TaskResult {
pub fn new(status: i32) -> Self {
Self { status }