Skip to content

Commit e018a93

Browse files
committed
perf: add missing types, cause they gona be accessing many times
1 parent a437062 commit e018a93

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

lib/rs/option.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def unwrap_or_else(&block)
5252
end
5353

5454
class Some < Rs::Option
55+
attr_reader :type
56+
5557
def self.[](type, &block)
5658
if !type.is_a?(Class)
5759
raise TypeError.new(type)
@@ -70,11 +72,11 @@ def self.[](type, &block)
7072
end
7173

7274
def inspect
73-
"Some[#{@value.class}] { #{@value.inspect} }"
75+
"Some[#{@type}] { #{@value.inspect} }"
7476
end
7577

7678
def ==(other)
77-
other.is_a?(Some) && @value == other.unwrap && @value.class == other.unwrap.class
79+
other.is_a?(Some) && @value == other.unwrap && @type == other.type
7880
end
7981

8082
def initialize(value)
@@ -83,6 +85,7 @@ def initialize(value)
8385
end
8486

8587
@value = value
88+
@type = value.class
8689
end
8790
end
8891

lib/rs/result.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def is_err_and(&block)
3636
end
3737

3838
def ok
39-
is_ok ? Some[@value.class] { @value } : None[@value_type]
39+
is_ok ? Some[@value_type] { @value } : None[@value_type]
4040
end
4141

4242
def err
43-
is_err ? Some[@error.class] { @error } : None[@error_type]
43+
is_err ? Some[@error_type] { @error } : None[@error_type]
4444
end
4545

4646
def expect(msg)
@@ -70,6 +70,7 @@ def unwrap_err
7070
end
7171

7272
class Ok < Rs::Result
73+
attr_accessor :value_type
7374
attr_accessor :error_type
7475

7576
def self.[](value_type, error_type, &block)
@@ -96,11 +97,11 @@ def self.[](value_type, error_type, &block)
9697
end
9798

9899
def inspect
99-
"Ok[#{@value.class}, #{@error_type}] { #{@value.inspect} }"
100+
"Ok[#{@value_type}, #{@error_type}] { #{@value.inspect} }"
100101
end
101102

102103
def ==(other)
103-
other.is_a?(Ok) && @value == other.unwrap && @value.class == other.unwrap.class && @error_type == other.error_type
104+
other.is_a?(Ok) && @value == other.unwrap && @value_type == other.value_type && @error_type == other.error_type
104105
end
105106

106107
def initialize(value)
@@ -109,12 +110,14 @@ def initialize(value)
109110
end
110111

111112
@value = value
113+
@value_type = value.class
112114
@error_type = Class
113115
end
114116
end
115117

116118
class Err < Rs::Result
117119
attr_accessor :value_type
120+
attr_accessor :error_type
118121

119122
def self.[](value_type, error_type, &block)
120123
if !value_type.is_a?(Class) || !error_type.is_a?(Class)
@@ -140,11 +143,11 @@ def self.[](value_type, error_type, &block)
140143
end
141144

142145
def inspect
143-
"Err[#{@value_type}, #{@error.class}] { #{@error.inspect} }"
146+
"Err[#{@value_type}, #{@error_type}] { #{@error.inspect} }"
144147
end
145148

146149
def ==(other)
147-
other.is_a?(Err) && @error == other.unwrap_err && @error.class == other.unwrap_err.class && @value_type == other.value_type
150+
other.is_a?(Err) && @error == other.unwrap_err && @value_type == other.value_type && @error_type == other.error_type
148151
end
149152

150153
def initialize(error)
@@ -153,6 +156,7 @@ def initialize(error)
153156
end
154157

155158
@error = error
159+
@error_type = error.class
156160
@value_type = Class
157161
end
158162
end

0 commit comments

Comments
 (0)