hello_rust 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 19e0bdf829a5269838f604eed25fd9847c8235d0e73ea352f74ab00de7893c37
4
+ data.tar.gz: 0cb3ce616c1b6f12cbaa1a6fabac9d9c71ce37da937291b58cbce5aef9a05cfa
5
+ SHA512:
6
+ metadata.gz: 0b0d3208bd9aee1752eac232c95effd903068b5901abfa4857a51e4c633772cc0e0c8d96400dafa65c81a554aa3960393e7b948e28b8d8ca242bfafe9d3f7903
7
+ data.tar.gz: d8bd43880dd9cea797e6f30b792fbde429d1538fbaa3da8fa828d765a8339f3d3a3197fe0ae9e14c270950875ed21bf32408dc81bf0e0f621400199d9e1be7f0
@@ -0,0 +1,17 @@
1
+ /target
2
+ Cargo.lock
3
+ /.bundle/
4
+ /.yardoc
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.gem
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
17
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,12 @@
1
+ [package]
2
+ name = "hello_rust_gem"
3
+ version = "0.1.0"
4
+ authors = ["irxground <irxnjhtchlnrw@gmail.com>"]
5
+ edition = "2018"
6
+ description = "a Ruby native extension using Rust with minimal dependencies"
7
+ homepage = "https://github.com/irxground/hello_rust_gem"
8
+ repository = "https://github.com/irxground/hello_rust_gem"
9
+ license = "MIT"
10
+
11
+ [lib]
12
+ crate-type = ["cdylib"]
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hello_rust.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rufo"
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hello_rust (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.0)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.0)
24
+ rufo (0.12.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ hello_rust!
31
+ rake (~> 12.0)
32
+ rspec (~> 3.0)
33
+ rufo
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 irxground
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,11 @@
1
+ # hello_rust_gem
2
+
3
+ This project aims to create a Ruby native extension using Rust with minimal dependencies.
4
+
5
+ ## Confirmed platform
6
+
7
+ - [x] Windows 10 (64bit) + RubyInstaller (x64)
8
+ - [x] Windows 10 (64bit) + RubyInstaller (x86)
9
+ - [ ] macOS Catalina
10
+ - [x] macOS Catalina + rbenv
11
+ - [x] Amazon linux 2 (x86_64) + rbenv
@@ -0,0 +1,12 @@
1
+ require "rspec/core/rake_task"
2
+ RSpec::Core::RakeTask.new(:spec)
3
+
4
+ require_relative "ext/build_task.rb"
5
+ BuildTask.new("hello_rust", __dir__, File.join(__dir__, "lib"))
6
+
7
+ task :fmt do
8
+ system "cargo fmt"
9
+ system "rufo ."
10
+ end
11
+
12
+ task :default => [:clobber, :install, :spec]
@@ -0,0 +1,15 @@
1
+ use std::process::Command;
2
+
3
+ fn rb_config(key: &str) -> String {
4
+ let output = Command::new("ruby")
5
+ .args(&["-e", &format!("print RbConfig::CONFIG['{}']", key)])
6
+ .output()
7
+ .expect("failed run ruby");
8
+
9
+ return String::from_utf8(output.stdout).unwrap();
10
+ }
11
+
12
+ fn main() {
13
+ println!("cargo:rustc-link-search={}", rb_config("libdir"));
14
+ println!("cargo:rustc-link-lib={}", rb_config("RUBY_SO_NAME"));
15
+ }
@@ -0,0 +1,6 @@
1
+ require_relative "./build_task"
2
+ BuildTask.new("hello_rust", File.expand_path("../..", __FILE__), ENV.fetch("RUBYLIBDIR"))
3
+
4
+ task :default => :install do
5
+ Rake::Task[:clean].invoke
6
+ end
@@ -0,0 +1,63 @@
1
+ require "json"
2
+ require "rake/tasklib"
3
+
4
+ class BuildTask < Rake::TaskLib
5
+ attr_reader :name, :lib_dir
6
+ attr_reader :manifest_file
7
+
8
+ def initialize(name, cargo_dir, lib_dir)
9
+ @name = name
10
+ @lib_dir = lib_dir
11
+ @manifest_file = File.join(cargo_dir, "Cargo.toml")
12
+
13
+ define_tasks
14
+ end
15
+
16
+ def define_tasks
17
+ target_so_dir = File.join(lib_dir, name)
18
+ target_so = File.join(target_so_dir, "#{name}.#{RbConfig::CONFIG["DLEXT"]}")
19
+
20
+ task :cargo do
21
+ unless system "cargo", "-V"
22
+ raise "`cargo` command not found. Please install Rust compiler: https://www.rust-lang.org/"
23
+ end
24
+ end
25
+
26
+ desc "Remove build cache"
27
+ task :clean => :cargo do
28
+ sh "cargo", "clean", "--manifest-path", manifest_file
29
+ end
30
+
31
+ desc "Remove build cache and compiled library"
32
+ task :clobber => :clean do
33
+ rm_f target_so
34
+ end
35
+
36
+ desc "Compile native extension"
37
+ task :build => :cargo do
38
+ env = {}
39
+ if RUBY_PLATFORM =~ /mingw/
40
+ env["RUSTUP_TOOLCHAIN"] = "stable-#{RbConfig::CONFIG["host_cpu"]}-pc-windows-gnu"
41
+ end
42
+ sh env, "cargo", "build", "--release", "--manifest-path", manifest_file
43
+ end
44
+
45
+ directory target_so_dir
46
+
47
+ desc "Place compiled library"
48
+ task :install => [:build, target_so_dir] do
49
+ cp cargo_output, target_so, preserve: true, verbose: true
50
+ end
51
+ end
52
+
53
+ def cargo_output
54
+ metadata = JSON.parse(`cargo metadata --format-version=1 --manifest-path #{manifest_file}`)
55
+ cargo_name = metadata.dig("packages", 0, "name")
56
+ filename = case RUBY_PLATFORM
57
+ when /mingw/; "#{cargo_name}.dll"
58
+ when /darwin/; "lib#{cargo_name}.dylib"
59
+ when /linux/; "lib#{cargo_name}.so"
60
+ end
61
+ File.join(metadata["target_directory"], "release", filename)
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ require "json"
2
+ # require_relative 'lib/hello_rust/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ cargo_metadata = JSON.parse(`cargo metadata --format-version=1`).dig("packages", 0)
6
+
7
+ spec.name = "hello_rust"
8
+ spec.version = cargo_metadata["version"]
9
+ spec.authors = ["irxground"]
10
+ spec.email = ["irxnjhtchlnrw@gmail.com"]
11
+
12
+ spec.summary = cargo_metadata["description"]
13
+ spec.homepage = cargo_metadata["homepage"] || cargo_metadata["repository"]
14
+ spec.license = cargo_metadata["license"]
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = cargo_metadata["repository"]
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ # spec.bindir = "exe"
25
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ # spec.require_paths = ["lib"]
27
+ spec.extensions = ["ext/Rakefile"]
28
+ spec.requirements << 'Rust compiler'
29
+ end
@@ -0,0 +1 @@
1
+ require "hello_rust/hello_rust"
@@ -0,0 +1,45 @@
1
+ use std::ffi;
2
+
3
+ mod ruby {
4
+ use std::ffi;
5
+
6
+ #[repr(transparent)]
7
+ pub struct Value(usize);
8
+
9
+ extern "C" {
10
+ // VALUE rb_define_module(const char *name)
11
+ fn rb_define_module(name: *const i8) -> Value;
12
+
13
+ // void rb_define_const(VALUE klass, const char *name, VALUE val)
14
+ fn rb_define_const(klass: Value, name: *const i8, val: Value);
15
+
16
+ // VALUE rb_str_new(const char *ptr, long len)
17
+ fn rb_str_new(ptr: *const u8, len: usize) -> Value;
18
+ }
19
+
20
+ #[inline]
21
+ pub fn define_module(name: &ffi::CStr) -> Value {
22
+ unsafe { rb_define_module(name.as_ptr()) }
23
+ }
24
+
25
+ #[inline]
26
+ pub fn define_const(klass: Value, name: &ffi::CStr, val: Value) {
27
+ unsafe { rb_define_const(klass, name.as_ptr(), val) }
28
+ }
29
+
30
+ #[inline]
31
+ pub fn new_string(str: &str) -> Value {
32
+ unsafe { rb_str_new(str.as_ptr(), str.len()) }
33
+ }
34
+ }
35
+
36
+ fn cstring(str: &str) -> ffi::CString {
37
+ ffi::CString::new(str).unwrap()
38
+ }
39
+
40
+ #[export_name = "Init_hello_rust"]
41
+ pub extern "C" fn init() {
42
+ let module = ruby::define_module(&cstring("HelloRust"));
43
+ let version = ruby::new_string(env!("CARGO_PKG_VERSION"));
44
+ ruby::define_const(module, &cstring("VERSION"), version)
45
+ }
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hello_rust
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - irxground
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - irxnjhtchlnrw@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/Rakefile
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - Cargo.toml
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - build.rs
30
+ - ext/Rakefile
31
+ - ext/build_task.rb
32
+ - hello_rust.gemspec
33
+ - lib/hello_rust.rb
34
+ - src/lib.rs
35
+ homepage: https://github.com/irxground/hello_rust_gem
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/irxground/hello_rust_gem
40
+ source_code_uri: https://github.com/irxground/hello_rust_gem
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements:
56
+ - Rust compiler
57
+ rubygems_version: 3.1.4
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: a Ruby native extension using Rust with minimal dependencies
61
+ test_files: []