Module: WGPU::NativeResource

Defined Under Namespace

Modules: ClassMethods, Lifecycle

Constant Summary collapse

GUARDED_METHOD_EXEMPTIONS =
[:initialize, :release, :released?, :handle, :label, :inspect].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#labelString? (readonly)

Returns the value of attribute label.

Returns:

  • (String, nil)


249
250
251
# File 'lib/wgpu/native_resource.rb', line 249

def label
  @label
end

Class Method Details

.included(base) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/wgpu/native_resource.rb', line 234

def self.included(base)
  guard = Module.new
  base.public_instance_methods(false).each do |method_name|
    next if GUARDED_METHOD_EXEMPTIONS.include?(method_name)

    guard.define_method(method_name) do |*args, **kwargs, &block|
      ensure_not_released!
      super(*args, **kwargs, &block)
    end
  end
  base.extend(ClassMethods)
  base.prepend(guard)
  base.prepend(Lifecycle)
end

Instance Method Details

#inspectString

Returns a concise lifecycle-oriented representation.

Returns:

  • (String)

    class, optional label, and release state



280
281
282
283
# File 'lib/wgpu/native_resource.rb', line 280

def inspect
  label_text = @label ? " label=#{@label.inspect}" : ""
  "#<#{self.class}#{label_text} released=#{released?}>"
end

#releasevoid

This method returns an undefined value.

Releases the native handle and unregisters its leak-tracking entry.



104
# File 'sig/wgpu.rbs', line 104

def release: () -> void

#released?Boolean

Reports whether the native handle has been released or is null.

Returns:

  • (Boolean)


253
254
255
256
257
258
259
# File 'lib/wgpu/native_resource.rb', line 253

def released?
  return true if @released
  return false unless instance_variable_defined?(:@handle)
  return true if @handle.nil?

  @handle.respond_to?(:null?) && @handle.null?
end

#use {|resource| ... } ⇒ Object

Yields this wrapper and always releases it when the block exits.

This is Ruby convenience API; WebGPU itself has no block-scoped resource primitive.

Yield Parameters:

Returns:

  • the block result



268
269
270
271
272
273
274
275
# File 'lib/wgpu/native_resource.rb', line 268

def use
  raise ArgumentError, "block is required" unless block_given?

  ensure_not_released!
  yield self
ensure
  release if block_given? && !released?
end