Skip to content

Commit 2a0c5af

Browse files
authored
Merge pull request #15 from ruby/bundle-rbs
Bundle RBS files
2 parents b481eb3 + ae6a273 commit 2a0c5af

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
source "https://rubygems.org"
22

33
gemspec
4+
5+
if RUBY_VERSION >= "3.0.0"
6+
gem "rbs", "~> 3.4"
7+
gem "rdoc"
8+
end

Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ Rake::TestTask.new(:test) do |t|
88
end
99

1010
task :default => :test
11+
12+
namespace :rbs do
13+
desc "Update embedded RDoc comments in RBS files"
14+
task :rdoc do
15+
sh "rm -rf tmp/rbs/ri"
16+
sh "mkdir -p tmp/rbs"
17+
sh "rdoc lib --format=ri -o tmp/rbs/ri"
18+
sh "rbs annotate --dir=tmp/rbs/ri --no-system --no-filename sig"
19+
end
20+
end

sig/mutex_m.rbs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# # mutex_m.rb
2+
#
3+
# When 'mutex_m' is required, any object that extends or includes Mutex_m will
4+
# be treated like a Mutex.
5+
#
6+
# Start by requiring the standard library Mutex_m:
7+
#
8+
# require "mutex_m"
9+
#
10+
# From here you can extend an object with Mutex instance methods:
11+
#
12+
# obj = Object.new
13+
# obj.extend Mutex_m
14+
#
15+
# Or mixin Mutex_m into your module to your class inherit Mutex instance methods
16+
# --- remember to call super() in your class initialize method.
17+
#
18+
# class Foo
19+
# include Mutex_m
20+
# def initialize
21+
# # ...
22+
# super()
23+
# end
24+
# # ...
25+
# end
26+
# obj = Foo.new
27+
# # this obj can be handled like Mutex
28+
#
29+
module Mutex_m
30+
def self.append_features: (Module cl) -> untyped
31+
32+
def self.define_aliases: (Module cl) -> untyped
33+
34+
def self.extend_object: (Object obj) -> untyped
35+
36+
public
37+
38+
def mu_extended: () -> untyped
39+
40+
# <!--
41+
# - mu_lock()
42+
# -->
43+
# See Thread::Mutex#lock
44+
#
45+
def mu_lock: () -> Thread::Mutex
46+
47+
# <!--
48+
# - mu_locked?()
49+
# -->
50+
# See Thread::Mutex#locked?
51+
#
52+
def mu_locked?: () -> bool
53+
54+
# <!--
55+
# - mu_synchronize(&block)
56+
# -->
57+
# See Thread::Mutex#synchronize
58+
#
59+
def mu_synchronize: [T] () { () -> T } -> T
60+
61+
# <!--
62+
# - mu_try_lock()
63+
# -->
64+
# See Thread::Mutex#try_lock
65+
#
66+
def mu_try_lock: () -> bool
67+
68+
# <!--
69+
# - mu_unlock()
70+
# -->
71+
# See Thread::Mutex#unlock
72+
#
73+
def mu_unlock: () -> Thread::Mutex
74+
75+
# <!--
76+
# - sleep(timeout = nil)
77+
# -->
78+
# See Thread::Mutex#sleep
79+
#
80+
def sleep: (?Numeric timeout) -> Integer?
81+
82+
alias locked? mu_locked?
83+
84+
alias lock mu_lock
85+
86+
alias unlock mu_unlock
87+
88+
alias try_lock mu_try_lock
89+
90+
alias synchronize mu_synchronize
91+
92+
private
93+
94+
def initialize: (*untyped args) -> untyped
95+
96+
def mu_initialize: () -> untyped
97+
end
98+
99+
Mutex_m::VERSION: String

test/test_rbs_types.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'test/unit'
2+
require 'mutex_m'
3+
begin
4+
require 'rbs/unit_test'
5+
rescue LoadError
6+
return
7+
end
8+
9+
module RBSTypeTest
10+
class Mutex_mInstanceTest < Test::Unit::TestCase
11+
include RBS::UnitTest::TypeAssertions
12+
13+
library 'mutex_m'
14+
testing "::Mutex_m"
15+
16+
def mu
17+
Object.new.tap do |o|
18+
o.extend Mutex_m
19+
end
20+
end
21+
22+
def test_mu_lock
23+
assert_send_type "() -> Thread::Mutex",
24+
mu, :mu_lock
25+
end
26+
27+
def test_mu_locked?
28+
mu = mu()
29+
assert_send_type "() -> false",
30+
mu, :mu_locked?
31+
mu.lock
32+
assert_send_type "() -> true",
33+
mu, :mu_locked?
34+
end
35+
36+
def test_mu_synchronize
37+
assert_send_type "() { () -> String } -> String",
38+
mu, :mu_synchronize do 'foo' end
39+
end
40+
41+
def test_mu_try_lock
42+
assert_send_type "() -> bool",
43+
mu, :mu_try_lock
44+
end
45+
46+
def test_mu_unlock
47+
mu = mu()
48+
mu.lock
49+
assert_send_type "() -> Thread::Mutex",
50+
mu, :mu_unlock
51+
end
52+
53+
def test_sleep
54+
mu = mu()
55+
mu.lock
56+
assert_send_type "(Integer) -> Integer?",
57+
mu, :sleep, 0
58+
assert_send_type "(Float) -> Integer?",
59+
mu, :sleep, 0.1
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)