Skip to main content

LwprintfObj

Struct LwprintfObj 

Source
pub struct LwprintfObj<T: CustomOutPut> { /* private fields */ }
Expand description

LwPRINTF object with custom output handler.

Implementations§

Source§

impl<T: CustomOutPut> LwprintfObj<T>

Source

pub fn new() -> Self

Create a new uninitialized LwPRINTF object.

Examples found in repository?
examples/print.rs (line 21)
19unsafe fn lwprintf_ex() {
20    println!("--- lwprintf_ex demo ---");
21    let mut lwobj = LwprintfObj::<StdOut>::new();
22    // initialize lwprintf object with custom output function
23    lwprintf_init_ex(&mut lwobj);
24
25    lwprintf_printf_ex(
26        lwobj.as_mut_ptr(),
27        b"Hello, printf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
28        1 as i32,
29        b"printf_ex\0".as_ptr(),
30    );
31
32    let mut buf = [0u8; 100];
33    let l = lwprintf_rs::lwprintf_snprintf_ex(
34        lwobj.as_mut_ptr(),
35        buf.as_mut_ptr() as *mut i8,
36        buf.len(),
37        b"Hello, snprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
38        2 as i32,
39        b"snprintf_ex\0".as_ptr(),
40    );
41    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
42    print!("{}", s);
43
44    let l = lwprintf_sprintf_ex!(
45        lwobj.as_mut_ptr(),
46        buf.as_mut_ptr() as *mut i8,
47        b"Hello, sprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
48        3 as i32,
49        b"sprintf_ex macro\0".as_ptr()
50    );
51    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
52    print!("{}", s);
53
54    unsafe extern "C" fn call_lwprintf_vprintf_ex(
55        lwobj: &mut LwprintfObj<StdOut>,
56        format: *const i8,
57        args: ...
58    ) {
59        // let args = args.as_va_list();
60        unsafe {
61            lwprintf_vprintf_ex(lwobj.as_mut_ptr(), format, args);
62        }
63    }
64
65    call_lwprintf_vprintf_ex(
66        &mut lwobj,
67        b"Hello, vprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
68        4 as i32,
69        b"vprintf_ex\0".as_ptr(),
70    );
71
72    unsafe extern "C" fn call_lwprintf_vsnprintf_ex(
73        lwobj: &mut LwprintfObj<StdOut>,
74        buf: *mut u8,
75        n: usize,
76        format: *const i8,
77        args: ...
78    ) -> i32 {
79        // let args = args.as_va_list();
80        unsafe { lwprintf_vsnprintf_ex(lwobj.as_mut_ptr(), buf as *mut i8, n, format, args) }
81    }
82
83    let l = call_lwprintf_vsnprintf_ex(
84        &mut lwobj,
85        buf.as_mut_ptr(),
86        buf.len(),
87        b"Hello, vsnprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
88        6 as i32,
89        b"vsnprintf_ex\0".as_ptr(),
90    );
91    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
92    print!("{}", s);
93
94    lwprintf_vprintf_ex_rust(
95        lwobj.as_mut_ptr(),
96        b"Hello, vprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
97        5 as i32,
98        b"vprintf_ex_rust\0".as_ptr(),
99    );
100
101    let l = lwprintf_vsnprintf_ex_rust(
102        lwobj.as_mut_ptr(),
103        buf.as_mut_ptr() as *mut i8,
104        buf.len(),
105        b"Hello, vsnprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
106        6 as i32,
107        b"vsnprintf_ex_rust\0".as_ptr(),
108    );
109    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
110    print!("{}", s);
111}
Source

pub fn as_mut_ptr(&mut self) -> *mut lwprintf_s

Get a mutable reference to the underlying lwprintf_t object.

This allows calling sys functions directly with the object.

Examples found in repository?
examples/print.rs (line 26)
19unsafe fn lwprintf_ex() {
20    println!("--- lwprintf_ex demo ---");
21    let mut lwobj = LwprintfObj::<StdOut>::new();
22    // initialize lwprintf object with custom output function
23    lwprintf_init_ex(&mut lwobj);
24
25    lwprintf_printf_ex(
26        lwobj.as_mut_ptr(),
27        b"Hello, printf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
28        1 as i32,
29        b"printf_ex\0".as_ptr(),
30    );
31
32    let mut buf = [0u8; 100];
33    let l = lwprintf_rs::lwprintf_snprintf_ex(
34        lwobj.as_mut_ptr(),
35        buf.as_mut_ptr() as *mut i8,
36        buf.len(),
37        b"Hello, snprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
38        2 as i32,
39        b"snprintf_ex\0".as_ptr(),
40    );
41    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
42    print!("{}", s);
43
44    let l = lwprintf_sprintf_ex!(
45        lwobj.as_mut_ptr(),
46        buf.as_mut_ptr() as *mut i8,
47        b"Hello, sprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
48        3 as i32,
49        b"sprintf_ex macro\0".as_ptr()
50    );
51    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
52    print!("{}", s);
53
54    unsafe extern "C" fn call_lwprintf_vprintf_ex(
55        lwobj: &mut LwprintfObj<StdOut>,
56        format: *const i8,
57        args: ...
58    ) {
59        // let args = args.as_va_list();
60        unsafe {
61            lwprintf_vprintf_ex(lwobj.as_mut_ptr(), format, args);
62        }
63    }
64
65    call_lwprintf_vprintf_ex(
66        &mut lwobj,
67        b"Hello, vprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
68        4 as i32,
69        b"vprintf_ex\0".as_ptr(),
70    );
71
72    unsafe extern "C" fn call_lwprintf_vsnprintf_ex(
73        lwobj: &mut LwprintfObj<StdOut>,
74        buf: *mut u8,
75        n: usize,
76        format: *const i8,
77        args: ...
78    ) -> i32 {
79        // let args = args.as_va_list();
80        unsafe { lwprintf_vsnprintf_ex(lwobj.as_mut_ptr(), buf as *mut i8, n, format, args) }
81    }
82
83    let l = call_lwprintf_vsnprintf_ex(
84        &mut lwobj,
85        buf.as_mut_ptr(),
86        buf.len(),
87        b"Hello, vsnprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
88        6 as i32,
89        b"vsnprintf_ex\0".as_ptr(),
90    );
91    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
92    print!("{}", s);
93
94    lwprintf_vprintf_ex_rust(
95        lwobj.as_mut_ptr(),
96        b"Hello, vprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
97        5 as i32,
98        b"vprintf_ex_rust\0".as_ptr(),
99    );
100
101    let l = lwprintf_vsnprintf_ex_rust(
102        lwobj.as_mut_ptr(),
103        buf.as_mut_ptr() as *mut i8,
104        buf.len(),
105        b"Hello, vsnprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
106        6 as i32,
107        b"vsnprintf_ex_rust\0".as_ptr(),
108    );
109    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
110    print!("{}", s);
111}

Trait Implementations§

Source§

impl<T: CustomOutPut> Default for LwprintfObj<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LwprintfObj<T>

§

impl<T> RefUnwindSafe for LwprintfObj<T>
where T: RefUnwindSafe,

§

impl<T> !Send for LwprintfObj<T>

§

impl<T> !Sync for LwprintfObj<T>

§

impl<T> Unpin for LwprintfObj<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for LwprintfObj<T>

§

impl<T> UnwindSafe for LwprintfObj<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.