rust_require 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 965d9bac57b7c7c943ecbd174a1e608f9d00ce22
4
- data.tar.gz: 1bc2d94473b2ebde3854f693fdaebe5adf3c9ede
3
+ metadata.gz: 4bc920f52a113f72975ec221fca1fb106493c481
4
+ data.tar.gz: c2217f4c5a0abcca147ec2c05deb321c25e728eb
5
5
  SHA512:
6
- metadata.gz: 613dbd5fed2cd485bb3f3cbeb64e27ab4f2a17c222a719d53b2d80322f3918909a3acaee7183af509e670ee1682a60a81c5e97c9f4ca1ac9af1f3de2eedc3614
7
- data.tar.gz: c20418ae1136f036af523885bcb88405f0496445335d2381ff4ef614378f8ebb3f30ba9a7f3604ff2b97d2906f1a89ec69fdcaa122c754448f18cf87c481b1ac
6
+ metadata.gz: 0145328db7639bb2535230934fdf1848e0a684245e31cd6a9ca6066483a64d47fb14f556d34c20ad61f0d7dcddfecd6e516486dda87598338555150631c39441
7
+ data.tar.gz: 466cd131e3527f9f5877735a94f291544f468eaa0772f43412a8ef80c4078ac7a8b509c23481721a98e2abf419b68ac5d4365067eda990752c96cbc51f388cea
@@ -35,11 +35,9 @@ module Rust
35
35
  gen = CWrapperGenerator.new(info_file)
36
36
 
37
37
  File.open(@tempfile, "w+") do |f|
38
- # add necessary extern crate definitions
38
+ # add necessary extern crate definitions #FIXME: if this proves to be unnecessary remove it
39
39
  f << <<-SRC
40
- #![allow(unused_features)]
41
- #![feature(libc,cstr_to_str)]
42
- extern crate libc;
40
+
43
41
  SRC
44
42
 
45
43
  # add the actual file content
@@ -16,25 +16,68 @@ module Rust
16
16
  @rust_type = "bool"
17
17
  end
18
18
 
19
- class Isize < Type
20
- @rust_type = "isize"
21
- def ffi_type; :int; end
19
+ class PrimitiveInteger < Type
20
+ def initialize
21
+ @bits = self.class.bits
22
+ @signed = self.class.signed
23
+ super
24
+ end
25
+
26
+ def self.bits; @bits; end
27
+ def self.signed; @signed; end
28
+
29
+ def rust_type_regex
30
+ @rust_type ? super : /.^/ # will never match
31
+ end
32
+
33
+ def type_check(num)
34
+ bounds = if @signed
35
+ (-2**(@bits-1)+1)..(2**(@bits-1)-1)
36
+ else
37
+ 0..(2**@bits-1)
38
+ end
39
+
40
+ raise ArgumentError, "#{num.inspect} is no integer." unless [Fixnum, Bignum].include? num.class
41
+ raise ArgumentError, "#{num} is not in the expected input range #{bounds}" unless bounds.include? num
42
+ end
43
+
44
+ def ruby_input_conversion(num)
45
+ type_check(num)
46
+ num
47
+ end
22
48
  end
23
49
 
24
- class Usize < Type
50
+ class Usize < PrimitiveInteger
25
51
  @rust_type = "usize"
26
- def ffi_type; :uint; end
52
+ @bits = FFI::Pointer.size * 8
53
+ @signed = false
54
+
55
+ def ffi_type; :ulong; end
27
56
  end
28
57
 
29
- # more metaprogramming!!
58
+ class Isize < PrimitiveInteger
59
+ @rust_type = "isize"
60
+ @bits = FFI::Pointer.size * 8
61
+ @signed = true
62
+
63
+ def ffi_type; :long; end
64
+ end
65
+
66
+ # metaprogramming!!
30
67
  %w[8 16 32 64].each do |x|
31
- usize = Class.new(Type) do
68
+ usize = Class.new(PrimitiveInteger) do
69
+ @bits = x.to_i
70
+ @signed = false
32
71
  @rust_type = "u#{x}"
72
+
33
73
  def ffi_type; rust_type.sub('u', 'uint').to_sym; end
34
74
  end
35
75
 
36
- isize = Class.new(Type) do
76
+ isize = Class.new(PrimitiveInteger) do
37
77
  @rust_type = "i#{x}"
78
+ @bits = x.to_i
79
+ @signed = true
80
+
38
81
  def ffi_type; rust_type.sub('i', 'int').to_sym; end
39
82
  end
40
83
 
@@ -51,11 +94,25 @@ module Rust
51
94
  def c_output_conversion(name); "#{name} as f64"; end
52
95
 
53
96
  def ffi_type; :double; end
97
+
98
+ def ruby_input_conversion(float)
99
+ bounds = -3.4028234663852886e+38 .. 3.4028234663852886e+38
100
+ raise ArgumentError, "#{float.inspect} is no Float." unless float.is_a? Float
101
+ raise ArgumentError, "#{float} is not in the expected input range #{bounds}" unless bounds.include? float
102
+ float
103
+ end
54
104
  end
55
105
 
56
106
  class F64 < Type
57
107
  @rust_type = 'f64'
58
108
  def ffi_type; :double; end
109
+
110
+ def ruby_input_conversion(float)
111
+ bounds = Float::MIN..Float::MAX
112
+ raise ArgumentError, "#{float.inspect} is no Float." unless float.is_a? Float
113
+ raise ArgumentError, "#{float} is not in the expected input range #{bounds}" unless bounds.include? float
114
+ float
115
+ end
59
116
  end
60
117
  end
61
118
  end
@@ -47,7 +47,7 @@ module Rust
47
47
  end
48
48
 
49
49
  def not_implemented
50
- raise NotImplementedError, '&str as rust output parameter is not supported, use String instead!'
50
+ raise NotImplementedError, '&mut str/&str as rust output parameter is not supported, use String instead!'
51
51
  end
52
52
 
53
53
  def c_output_type
@@ -71,8 +71,8 @@ module Rust
71
71
  def ruby_input_conversion(str)
72
72
  str.encode!(Encoding::UTF_8)
73
73
  len = str.bytesize
74
- start = FFI::MemoryPointer.from_string(str).address
75
- Rust::Slice.from(start, len)
74
+ start = FFI::MemoryPointer.from_string(str)
75
+ Rust::Slice.from(start.address, len)
76
76
  end
77
77
  end
78
78
  end
@@ -1,3 +1 @@
1
- module Rust
2
- VERSION = "0.2.0"
3
- end
1
+ VERSION = "0.2.1"
data/lib/rust_require.rb CHANGED
@@ -3,7 +3,10 @@ require 'ffi'
3
3
  require 'active_support/core_ext/string/inflections'
4
4
 
5
5
  # Libs
6
- require_relative 'rust_require/version.rb'
6
+ module Rust
7
+ require_relative 'rust_require/version.rb'
8
+ end
9
+
7
10
  require_relative 'rust_require/rust_require.rb'
8
11
  require_relative 'rust_require/rustc.rb'
9
12
  require_relative 'rust_require/types.rb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rust_require
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Lackner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-09 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi