Class: Rust::Types::String
- Inherits:
-
Type
- Object
- Type
- Rust::Types::String
show all
- Defined in:
- lib/rust_require/types/string.rb
Instance Attribute Summary
Attributes inherited from Type
#rust_type
Instance Method Summary
collapse
Methods inherited from Type
#c_type, #ffi_input_type, #ffi_output_type, #initialize, rust_type, #rust_type_regex
Instance Method Details
42
43
44
|
# File 'lib/rust_require/types/string.rb', line 42
def c_input_conversion(slice)
not_implemented
end
|
12
13
14
|
# File 'lib/rust_require/types/string.rb', line 12
def c_input_type
not_implemented
end
|
#c_output_conversion(name) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rust_require/types/string.rb', line 18
def c_output_conversion(name)
<<-CODE
{
let mut #{name} = #{name};
// deallocate unused capacity if any
if #{name}.len() != #{name}.capacity() {
unsafe {
let ptr = #{name}.as_mut_vec().as_mut_ptr();
std::rt::heap::deallocate(
ptr.offset(#{name}.len() as isize),
#{name}.capacity() - #{name}.len(),
std::mem::min_align_of::<u8>()
);
}
}
let tuple = (#{name}.as_ptr(), #{name}.len());
std::mem::forget(#{name});
tuple
}
CODE
end
|
#c_output_type ⇒ Object
16
|
# File 'lib/rust_require/types/string.rb', line 16
def c_output_type; '(*const u8,usize)'; end
|
#ffi_type ⇒ Object
46
|
# File 'lib/rust_require/types/string.rb', line 46
def ffi_type; Rust::Slice.by_value; end
|
#not_implemented ⇒ Object
8
9
10
|
# File 'lib/rust_require/types/string.rb', line 8
def not_implemented
raise NotImplementedError, 'String as rust input type is not supported, use &str instead!'
end
|
53
54
55
|
# File 'lib/rust_require/types/string.rb', line 53
def ruby_input_conversion(str)
not_implemented
end
|
#ruby_output_conversion(slice) ⇒ Object
48
49
50
51
|
# File 'lib/rust_require/types/string.rb', line 48
def ruby_output_conversion(slice)
(start,len) = slice.unpack
start.read_string(len.to_i).force_encoding("UTF-8")
end
|