-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnu_io_channel.rs
170 lines (151 loc) · 5.12 KB
/
gnu_io_channel.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![allow(non_camel_case_types)]
extern crate alloc;
/// [struct GNU::GIOChannel](https://github.com/GNOME/glib/blob/master/glib/giochannel.h)
///
/// Based on a comment in [#RFC-1449 Add language support for bitfields](https://github.com/rust-lang/rfcs/pull/1449#issuecomment-318482265).
///
/// Layout (only the flags):
///
/// ```
/// 7 3 0
/// ╔═══╤═╤═╪═╤═╤═╤═╗
/// ║ │I│I│I│C│D│U║
/// ║ │S│W│R│O│E│B║ Flags
/// ║0 0│ │ │ │U│ │ ║
/// ╚═══╧═╧═╧═╧═╧═╧═╝
/// UB = Use Buffer
/// DE = Do Encode
/// COU = Close On Unref
/// IR = Is Readable
/// IW = Is Writable
/// IS = Is Seekable
/// ```
#[bitfield::bitfield(8)]
#[derive(Display)]
struct Flags(Flag);
#[derive(Copy, Clone, Debug, bitfield::Flags)]
#[repr(u8)]
enum Flag {
/// The encoding uses the buffers.
UseBuffer,
/// The encoding uses the `GIConv` converters.
DoEncode,
/// Close the channel on final unref.
CloseOnUnref,
/// Cached `GIOFlag`.
IsReadable,
/// Cached `GIOFlag`.
IsWritable,
/// Cached `GIOFlag`.
IsSeekable
// Bits 6 - 7 are reserved.
}
/// See https://github.com/GNOME/glib/blob/master/glib/giochannel.h
#[repr(C)]
struct GIOChannel {
ref_count: gint,
funcs: *const u8,
encoding: *const gchar,
read_cd: GIConv,
write_cd: GIConv,
/// String which indicates the end of a line of text.
line_term: *const gchar,
/// So we can have null in the line term.
line_term_len: guint,
buf_size: gsize,
/// Raw data from the channel.
read_buf: *const GString,
/// Channel data converted to UTF-8.
encoded_read_buf: *const GString,
/// Data ready to be written to the file.
write_buf: *const GString,
/// UTF-8 partial characters, null terminated.
partial_write_buf: [gchar; 6],
// Group the flags together, immediately after `partial_write_buf`, to save memory.
flags: Flags,
reserved1: gpointer,
reserved2: gpointer
}
/// See https://github.com/GNOME/glib/blob/master/glib/gtypes.h
type gchar = i8;
type gint = i32;
type gpointer = *const u8;
type gsize = usize;
type guint = u32;
/// See https://github.com/GNOME/glib/blob/master/glib/gconvert.h
type GIConv = *const u8;
/// See https://github.com/GNOME/glib/blob/master/glib/gstring.h
#[repr(C)]
struct GString {
str: *const gchar,
len: gsize,
allocated_len: gsize
}
fn main() {
let mut channel = GIOChannel {
ref_count: 0, funcs: 0 as _, encoding: 0 as _, read_cd: 0 as _, write_cd: 0 as _,
line_term: 0 as _, line_term_len: 0, buf_size: 0, read_buf: 0 as _,
encoded_read_buf: 0 as _, write_buf: 0 as _, partial_write_buf: [0; 6],
reserved1: 0 as _, reserved2: 0 as _,
flags: Flags::new()
+ Flag::DoEncode // Same as: `.set_flag(Flag::DoEncode, true)`
+ Flag::IsWritable // Same as: `.set_flag(Flag::IsWritable, true)`
};
// Check and update flag.
if channel.flags.has(Flag::DoEncode) {
channel.flags -= Flag::DoEncode; // Same as: `.set_flag(Flag::DoEncode, false)`
}
// Invert flag.
channel.flags ^= Flag::CloseOnUnref; // Same as: `.invert_flag(Flag::CloseOnUnref)`
assert_eq!(&format!("{}", &channel.flags), "CloseOnUnref | IsWritable");
println!("Flags: {}", &channel.flags);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size() {
#[cfg(target_pointer_width = "32")]
assert_eq!(
core::mem::size_of::<GIOChannel>(),
4 + // ref_count: gint,
4 + // funcs: *const u8,
4 + // encoding: *const gchar,
4 + // read_cd: GIConv,
4 + // write_cd: GIConv,
4 + // line_term: *const gchar,
4 + // line_term_len: guint,
4 + // buf_size: gsize,
4 + // read_buf: *const GString,
4 + // encoded_read_buf: *const GString,
4 + // write_buf: *const GString,
6 + // partial_write_buf: [gchar; 6],
1 + // flags: Flags,
(1) + // -- alignment --
4 + // reserved1: gpointer,
4 // reserved2: gpointer
);
#[cfg(target_pointer_width = "64")]
assert_eq!(
core::mem::size_of::<GIOChannel>(),
4 + // ref_count: gint,
(4) + // -- alignment --
8 + // funcs: *const u8,
8 + // encoding: *const gchar,
8 + // read_cd: GIConv,
8 + // write_cd: GIConv,
8 + // line_term: *const gchar,
4 + // line_term_len: guint,
(4) + // -- alignment --
8 + // buf_size: gsize,
8 + // read_buf: *const GString,
8 + // encoded_read_buf: *const GString,
8 + // write_buf: *const GString,
6 + // partial_write_buf: [gchar; 6],
1 + // flags: Flags,
(1) + // -- alignment --
8 + // reserved1: gpointer,
8 // reserved2: gpointer
);
}
}