Module: Rust

Defined in:
lib/rust_require.rb,
lib/rust_require/rustc.rb,
lib/rust_require/types.rb,
lib/rust_require/rust_require.rb,
lib/rust_require/types/string.rb,
lib/rust_require/types/primitives.rb,
lib/rust_require/c_wrapper_generator.rb,
lib/rust_require/c_wrapper_generator.rb,
lib/rust_require/ruby_wrapper_generator.rb

Overview

deactivated for now, maybe reactivated later with explicit conversion

Defined Under Namespace

Modules: Types Classes: CWrapperGenerator, RubyWrapperGenerator, Rustc, Slice

Constant Summary collapse

ALREADY_REQUIRED =

Hash to keep record of already required files

Hash.new(false)

Class Method Summary collapse

Class Method Details

.already_required?(comb) ⇒ Boolean

checks if the file/mod combination has already been required

Returns:

  • (Boolean)


73
74
75
# File 'lib/rust_require/rust_require.rb', line 73

def self.already_required?(comb)
  ALREADY_REQUIRED[comb]
end

.check_file(file_path) ⇒ Object

This checks if file_name is a valid .rs file

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/rust_require/rust_require.rb', line 66

def self.check_file(file_path)
  raise ArgumentError, 'input must be a String object' unless file_path.is_a? String
  raise LoadError, "file #{file_path} not found"       unless File.exists? file_path
  raise NameError, 'input file must be a .rs file'     unless file_path.end_with? '.rs'
end

.create_subfolder(crate_name, dir_name) ⇒ Object

This creates a subfolder ‘.rust_require/#crate_name’ in dir_name to store intermediate files to cache compilation results



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rust_require/rust_require.rb', line 84

def self.create_subfolder(crate_name, dir_name)
  # path of the dirs to be created
  new_dir_paths = []
  new_dir_paths << "#{dir_name}/.rust_require"
  new_dir_paths << "#{dir_name}/.rust_require/#{crate_name}"

  new_dir_paths.each do |path|
    unless Dir.exists? path
      # mkdir with permissions: rwx-rx-r
      Dir.mkdir path, 0754
    end
  end

  # return the newly created dir path
  new_dir_paths[1]
end

.register_file(comb) ⇒ Object

registers a file/mod combination as ‘already_required’



78
79
80
# File 'lib/rust_require/rust_require.rb', line 78

def self.register_file(comb)
  ALREADY_REQUIRED[comb] = true
end

.require_rust_file(file_path, mod) ⇒ Object

file_path: String (path to .rs file) mod: Module/Class (Module which requires the .rs file)



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
# File 'lib/rust_require/rust_require.rb', line 12

def self.require_rust_file(file_path, mod)
  # type check haha
  check_file file_path

  # make the path absolute
  file_path = File.absolute_path file_path

  # check if the file was already required by mod
  return if already_required? [file_path,mod]

  register_file [file_path,mod]

  # compute rust crate name 
  # file name without .rs extension
  crate_name = File.basename(file_path, '.rs')

  # path of dir containing file_name
  dir_name = File.dirname(file_path)

  # in case of ../blabla/mod.rs
  if crate_name == 'mod'
    crate_name = dir_name.split("/").last
  end

  # create .rust_require/#{file_name} subfolder
  subdir = create_subfolder(crate_name, dir_name)

  # TODO: insert check for unmodified input here

  # location of info.json
  info_file_path = "#{subdir}/info.json"

  # Use Rustc to create wrappers and compile the file + wrappers
  rustc = Rustc.new(file_path)
  rustc.subdir         = subdir
  rustc.info_file_path = info_file_path
  rustc.output_path    = "#{subdir}/lib#{File.basename(file_path, '.rs')}.so"

  info_file = rustc.create_wrapper
  rustc.compile

  # Use RubyWrapperGenerator to make items from the compiled
  # lib available in mod
  gen = RubyWrapperGenerator.new
  gen.info_file = info_file
  gen.rust_lib  = rustc.output_path
  gen.include_lib(mod)

  true #explicit return value
end