Skip to content

Commit

Permalink
Feat: Try split sub part of PyTty into detailed class.
Browse files Browse the repository at this point in the history
  • Loading branch information
wychlw committed Sep 5, 2024
1 parent d55841c commit 9b6173f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ pub mod util {
pub mod pythonapi {
pub mod shell_like;

pub mod pyshell;

// pub mod testapi;
}

use pyo3::prelude::*;
use pythonapi::shell_like::PyTty;
use pythonapi::{pyshell::PyShell, shell_like::PyTty};

#[pymodule]
fn tester(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyTty>()?;
m.add_class::<PyShell>()?;
Ok(())
}
30 changes: 30 additions & 0 deletions src/pythonapi/pyshell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use pyo3::{exceptions::PyTypeError, pyclass, pymethods, PyResult};

use crate::{term::shell::Shell, util::anybase::heap_raw};

use super::shell_like::{PyTty, PyTtyWrapper, TtyType};

#[pyclass(extends=PyTty, subclass)]
pub struct PyShell {}

#[pymethods]
impl PyShell {
#[new]
#[pyo3(signature = (shell=None))]
fn py_new(shell: Option<&str>) -> PyResult<(Self, PyTty)> {
let shell = Shell::build(shell);
if let Err(e) = shell {
return Err(PyTypeError::new_err(e.to_string()));
}
let shell = shell.unwrap();
let shell = Box::new(shell) as TtyType;
Ok((
PyShell {},
PyTty::build(
PyTtyWrapper {
tty: heap_raw(shell),
},
),
))
}
}
23 changes: 14 additions & 9 deletions src/pythonapi/shell_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use crate::{
shell::Shell,
ssh::Ssh,
tty::{DynTty, WrapperTty},
},
}, util::anybase::heap_raw,
};

type TtyType = DynTty;
pub type TtyType = DynTty;

struct PyTtyWrapper {
tty: *mut TtyType,
pub struct PyTtyWrapper {
pub tty: *mut TtyType,
}

impl PyTtyWrapper {
Expand Down Expand Up @@ -56,11 +56,19 @@ impl PyTtyWrapper {

unsafe impl Send for PyTtyWrapper {}

#[pyclass]
#[pyclass(subclass)]
pub struct PyTty {
inner: PyTtyWrapper,
}

impl PyTty {
pub fn build(inner: PyTtyWrapper) -> Self {
PyTty {
inner
}
}
}

#[derive(Deserialize)]
struct PyTtyConf {
// unwrapable
Expand All @@ -77,10 +85,6 @@ struct PyTtyShellConf {
shell: Option<String>,
}

fn heap_raw<T>(t: T) -> *mut T {
Box::into_raw(Box::new(t))
}

fn handel_wrap(inner: &mut Option<PyTtyWrapper>, be_wrapped: Option<&mut PyTty>) -> PyResult<()> {
if be_wrapped.is_none() {
return Err(PyTypeError::new_err(
Expand Down Expand Up @@ -402,3 +406,4 @@ impl PyTty {
}
}
}

4 changes: 4 additions & 0 deletions src/util/anybase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ macro_rules! dyn_into {
$x.into_any().downcast::<$t>()
};
}

pub fn heap_raw<T>(t: T) -> *mut T {
Box::into_raw(Box::new(t))
}

0 comments on commit 9b6173f

Please sign in to comment.