feature: add derive macro for auto-CharDevice

This commit is contained in:
2021-11-11 21:15:12 +02:00
parent 41ffd0ddb7
commit 47ef7e29fe
8 changed files with 98 additions and 49 deletions
+38
View File
@@ -0,0 +1,38 @@
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(TtyCharDevice)]
pub fn derive_tty_char_device(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
if !ast.generics.params.is_empty() {
panic!(
"Derived TtyCharDevice cannot have generic parameters: {:?}",
ast.ident
);
}
let ident = ast.ident;
quote! {
impl vfs::CharDevice for #ident {
fn read(&self, blocking: bool, data: &mut [u8]) -> Result<usize, libsys::error::Errno> {
assert!(blocking);
crate::dev::tty::TtyDevice::line_read(self, data)
}
fn write(&self, blocking: bool, data: &[u8]) -> Result<usize, libsys::error::Errno> {
assert!(blocking);
crate::dev::tty::TtyDevice::line_write(self, data)
}
fn ioctl(&self, cmd: libsys::ioctl::IoctlCmd, ptr: usize, len: usize) ->
Result<usize, libsys::error::Errno>
{
crate::dev::tty::TtyDevice::tty_ioctl(self, cmd, ptr, len)
}
}
}.into()
}