mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Use Box for Wrapper implementation
This won't raise on invalid casts. It should reduce the amount of instantiated types and methods.
This commit is contained in:
parent
18a03b02fe
commit
e209fe3eb5
2 changed files with 4 additions and 32 deletions
|
@ -10,9 +10,4 @@ Spectator.describe Spectator::Wrapper do
|
|||
wrapper = described_class.new(Int32)
|
||||
expect(wrapper.get { Int32 }).to eq(Int32)
|
||||
end
|
||||
|
||||
it "raises on invalid cast" do
|
||||
wrapper = described_class.new(42)
|
||||
expect { wrapper.get(String) }.to raise_error(TypeCastError)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,18 +9,17 @@ module Spectator
|
|||
# value = wrapper.get(String)
|
||||
# ```
|
||||
struct Wrapper
|
||||
@value : TypelessValue
|
||||
@pointer : Void*
|
||||
|
||||
# Creates a wrapper for the specified value.
|
||||
def initialize(value)
|
||||
@value = Value.new(value)
|
||||
@pointer = Box.box(value)
|
||||
end
|
||||
|
||||
# Retrieves the previously wrapped value.
|
||||
# The *type* of the wrapped value must match otherwise an error will be raised.
|
||||
def get(type : T.class) : T forall T
|
||||
value = @value.as(Value(T))
|
||||
value.get
|
||||
Box(T).unbox(@pointer)
|
||||
end
|
||||
|
||||
# Retrieves the previously wrapped value.
|
||||
|
@ -35,29 +34,7 @@ module Spectator
|
|||
# type = wrapper.get { Int32 } # Returns Int32
|
||||
# ```
|
||||
def get(& : -> T) : T forall T
|
||||
value = @value.as(Value(T))
|
||||
value.get
|
||||
end
|
||||
|
||||
# Base type that generic types inherit from.
|
||||
# This provides a common base type,
|
||||
# since Crystal doesn't support storing an `Object` (yet).
|
||||
# Instances of this type must be downcast to `Value` to be useful.
|
||||
private abstract class TypelessValue
|
||||
end
|
||||
|
||||
# Generic value wrapper.
|
||||
# Simply holds a value and inherits from `TypelessValue`,
|
||||
# so that all types of this class can be stored as one.
|
||||
private class Value(T) < TypelessValue
|
||||
# Creates the wrapper with the specified value.
|
||||
def initialize(@value : T)
|
||||
end
|
||||
|
||||
# Retrieves the wrapped value.
|
||||
def get : T
|
||||
@value
|
||||
end
|
||||
Box(T).unbox(@pointer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue