torch_sys/
io.rs

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
106
107
108
109
110
111
use libc::{c_void, size_t};
use std::io::{Read, Seek, SeekFrom, Write};

pub trait ReadStream: Read + Seek {}

#[no_mangle]
extern "C" fn tch_write_stream_destructor(stream_ptr: *mut c_void) {
    unsafe {
        let boxed_stream: Box<Box<dyn Write>> = Box::from_raw(stream_ptr as *mut Box<dyn Write>);
        drop(boxed_stream)
    }
}

#[no_mangle]
extern "C" fn tch_write_stream_write(
    stream_ptr: *mut c_void,
    buf: *const u8,
    size: size_t,
    out_size: *mut size_t,
) -> bool {
    unsafe {
        let boxed_stream = stream_ptr as *mut Box<dyn Write>;
        let buffer = std::slice::from_raw_parts(buf, size);
        match (*boxed_stream).write(buffer) {
            Ok(x) => {
                *out_size = x;
                true
            }
            Err(_) => false,
        }
    }
}

#[no_mangle]
extern "C" fn tch_read_stream_destructor(stream_ptr: *mut c_void) {
    unsafe {
        let boxed_stream: Box<Box<dyn ReadStream>> =
            Box::from_raw(stream_ptr as *mut Box<dyn ReadStream>);
        drop(boxed_stream)
    }
}

#[no_mangle]
extern "C" fn tch_read_stream_stream_position(stream_ptr: *mut c_void, current: *mut u64) -> bool {
    unsafe {
        let boxed_stream = stream_ptr as *mut Box<dyn ReadStream>;
        match (*boxed_stream).stream_position() {
            Ok(ret) => {
                *current = ret;
                true
            }
            Err(_) => false,
        }
    }
}

#[no_mangle]
extern "C" fn tch_read_stream_seek_start(
    stream_ptr: *mut c_void,
    pos: u64,
    new_pos: *mut u64,
) -> bool {
    unsafe {
        let boxed_stream = stream_ptr as *mut Box<dyn ReadStream>;
        match (*boxed_stream).seek(SeekFrom::Start(pos)) {
            Ok(ret) => {
                *new_pos = ret;
                true
            }
            Err(_) => false,
        }
    }
}

#[no_mangle]
extern "C" fn tch_read_stream_seek_end(
    stream_ptr: *mut c_void,
    pos: i64,
    new_pos: *mut u64,
) -> bool {
    unsafe {
        let boxed_stream = stream_ptr as *mut Box<dyn ReadStream>;
        match (*boxed_stream).seek(SeekFrom::End(pos)) {
            Ok(ret) => {
                *new_pos = ret;
                true
            }
            Err(_) => false,
        }
    }
}

#[no_mangle]
extern "C" fn tch_read_stream_read(
    stream_ptr: *mut c_void,
    buf: *mut u8,
    size: size_t,
    read_size: *mut size_t,
) -> bool {
    unsafe {
        let boxed_stream = stream_ptr as *mut Box<dyn ReadStream>;
        let buffer = std::slice::from_raw_parts_mut(buf, size);
        match (*boxed_stream).read(buffer) {
            Ok(ret) => {
                *read_size = ret;
                true
            }
            Err(_) => false,
        }
    }
}