Skip to content

Commit 220483b

Browse files
authored
Merge pull request #1 from nilium/push-kmktrmmmmmuy
Update for Ruby 3.3-ish
2 parents e177b03 + 63d8be7 commit 220483b

6 files changed

Lines changed: 39 additions & 32 deletions

File tree

README.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,21 @@ Intro
88
-----
99

1010
Snow-Data is a simple gem for dealing with memory and defining structs in a
11-
C-like way. Incidentally, it's also hideously unsafe, so everything is tainted
12-
by default. You'll thank me for this later, even if almost every operation does
13-
bounds-checking where possible to ensure you're not being a horrible person.
11+
C-like way. It's also hideously unsafe, so everything is tainted by default.
1412

1513
For more information on usage, see the rdoc documentation for Snow::Memory
16-
and Snow::CStruct, as it explains the important things. Like CStructs. And how
17-
to talk to people. Ok, it can't help you with that.
18-
19-
_ALLONS-Y!_
20-
14+
and Snow::CStruct, as it should explain most of the important things.
2115

2216
Example
2317
-------
2418

25-
For those wanting a quick-ish example of using snow-data, I'll include one here
26-
showing you how you might define a few structs, including a Vec3, Vec2, Color,
27-
and Vertex and working with those.
19+
For those wanting a quick-ish example of using snow-data, the following example
20+
shows how to define a few structs. These are a Vec3, Vec2, Color, and Vertex,
21+
which may be common in game code.
2822

29-
Bear in mind that, down the road, it will also be possible to assign snow-math
30-
types to these as well (provided they use the same underlying types), though I
31-
wouldn't use this for defining data types for anything other than transit to
32-
another API that expects its data in a format like this.
33-
34-
How you use it, ultimately, is really up to you.
23+
In practice, this tends not to be highly useful except for interacting with
24+
some APIs over FFI, such as OpenGL. That said, if it happens to be useful,
25+
all the better.
3526

3627
#!/usr/bin/env ruby -w
3728

@@ -42,11 +33,12 @@ How you use it, ultimately, is really up to you.
4233
# it helps to illustrate that you can specify alignment).
4334
Vec3 = Snow::CStruct[:Vec3, 'x: float :4; y: float :4; z: float :4']
4435
Vec2 = Snow::CStruct[:Vec2, 'x: float :4; y: float :4']
36+
4537
# ui8 is shorthand for uint8_t -- you can write either, and the documentation
4638
# for CStruct::new explains the short- and long-form names for each primitive
4739
# type provided by Snow-Data. Further, CStructs defined with a name, as with
4840
# Vec3, Vec2, and Color, have getters and setters defined in the Memory class
49-
# and are usable as member types, as I'll show below.
41+
# and are usable as member types (see below).
5042
Color = Snow::CStruct[:Color, 'r: ui8; g: ui8; b: ui8; a: ui8']
5143

5244
# Define a vertex type whose members are all also 4-byte aligned. The vertex
@@ -69,7 +61,7 @@ How you use it, ultimately, is really up to you.
6961
VERTEX_DESCRIPTION
7062
end
7163

72-
# So let's create a vertex.
64+
# Now create a vertex:
7365
a_vertex = Vertex.new { |v|
7466
v.position = Vec3.new { |p| p.x = 1; p.y = 2; p.z = 3 }
7567
v.normal = Vec3.new { |n| n.x = 0.707107; n.y = 0; n.z = 0.707107 }
@@ -83,7 +75,7 @@ How you use it, ultimately, is really up to you.
8375

8476
puts "Our vertex:\n#{stringify_vertex a_vertex}"
8577

86-
# For kicks, let's create an array.
78+
# Create an array of 64 vertices:
8779
some_vertices = Vertex[64]
8880

8981
# And set all vertices to the above vertex.

ext/extconf.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
require 'mkmf'
77

8-
# Compile as C99
9-
$CFLAGS += " -std=c99 -Wall -pedantic"
10-
118
OptKVPair = Struct.new(:key, :value)
129

1310
option_mappings = {
@@ -53,7 +50,7 @@
5350
end
5451

5552
$CFLAGS += ' -DSD_ALLOW_ALLOCA' if options[:allow_alloca]
56-
$CFLAGS += ' -DSD_SD_WARN_ON_IMPLICIT_COPY_SIZE' if options[:warn_implicit_size]
53+
$CFLAGS += ' -DSD_WARN_ON_IMPLICIT_COPY_SIZE' if options[:warn_implicit_size]
5754
$CFLAGS += ' -DSD_WARN_ON_NO_BYTESIZE_METHOD' if options[:warn_no_bytesize]
5855
$CFLAGS += ' -DSD_VERBOSE_COPY_LOG' if options[:debug_memory_copy]
5956
$CFLAGS += ' -DSD_VERBOSE_MALLOC_LOG' if options[:debug_allocations]

ext/snow-data/snow-data.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "ruby.h"
8+
#include <limits.h>
89
#include <stdint.h>
910
#include <stdio.h>
1011

@@ -1668,7 +1669,9 @@ static VALUE sd_memory_copy(int argc, VALUE *argv, VALUE self)
16681669
size_t destination_offset;
16691670
size_t byte_size;
16701671
size_t self_byte_size;
1672+
#if defined(SD_WARN_ON_NO_BYTESIZE_METHOD) || defined(SD_WARN_ON_IMPLICIT_COPY_SIZE)
16711673
int source_is_data = 0;
1674+
#endif
16721675

16731676
sd_check_null_block(self);
16741677
rb_check_frozen(self);
@@ -1691,11 +1694,13 @@ static VALUE sd_memory_copy(int argc, VALUE *argv, VALUE self)
16911694
}
16921695
}
16931696

1694-
if (RTEST(rb_obj_is_kind_of(sd_source, rb_cData))) {
1697+
if (RTEST(rb_obj_is_kind_of(sd_source, rb_cObject))) {
16951698
/* Otherwise extract a pointer from the object if it's a Data object */
16961699
const struct RData *source_data = RDATA(sd_source);
16971700
source_pointer = ((const uint8_t *)source_data->data);
1701+
#if defined(SD_WARN_ON_NO_BYTESIZE_METHOD) || defined(SD_WARN_ON_IMPLICIT_COPY_SIZE)
16981702
source_is_data = 1;
1703+
#endif
16991704
} else if (RTEST(rb_obj_is_kind_of(sd_source, rb_cNumeric))) {
17001705
/* Otherwise, if it's a Numeric, try to convert what is assumed to be an
17011706
address to a pointer */
@@ -1890,7 +1895,8 @@ static VALUE sd_align_size(int argc, VALUE *argv, VALUE self)
18901895
void Init_snowdata_bindings(void)
18911896
{
18921897
VALUE sd_snow_module = rb_define_module("Snow");
1893-
VALUE sd_memory_klass = rb_define_class_under(sd_snow_module, "Memory", rb_cData);
1898+
VALUE sd_memory_klass = rb_define_class_under(sd_snow_module, "Memory", rb_cObject);
1899+
rb_undef_alloc_func(sd_memory_klass);
18941900

18951901
kSD_IVAR_BYTESIZE = rb_intern("@__bytesize__");
18961902
kSD_IVAR_ALIGNMENT = rb_intern("@__alignment__");

justfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[positional-arguments]
2+
install *args: build
3+
gem uninstall snow-data --version "$(just version)"
4+
gem install "$@" "snow-data-$(just version).gem"
5+
6+
build:
7+
rm -fv "snow-data-$(just version).gem"
8+
gem build
9+
10+
version:
11+
#!/usr/bin/env ruby
12+
require File.expand_path('lib/snow-data/version.rb', Dir.pwd)
13+
$stdout.write Snow::SNOW_DATA_VERSION

lib/snow-data/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ module Snow
77
#
88
# The snow-data version string.
99
#
10-
SNOW_DATA_VERSION = '1.3.1'.freeze
10+
SNOW_DATA_VERSION = '1.4.0'.freeze
1111

1212
end

snow-data.gemspec

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require File.expand_path('../lib/snow-data/version.rb', __FILE__)
77
Gem::Specification.new { |s|
88
s.name = 'snow-data'
99
s.version = Snow::SNOW_DATA_VERSION
10-
s.date = '2013-07-20'
10+
s.date = '2025-04-17'
1111
s.summary = "Snow-Data: for working with memory like you've got nothing to lose."
1212
s.description = <<-EOS
1313
Snow-Data is a gem for allocating memory and working with existing blocks of
@@ -16,14 +16,13 @@ also provides functionality for defining C-struct classes, including those with
1616
other structs as members.
1717
EOS
1818
s.authors = [ 'Noel Raymond Cower' ]
19-
s.email = 'ncower@gmail.com'
19+
s.email = 'ncower@nil.dev'
2020
s.files = Dir.glob('lib/**/*.rb') +
2121
Dir.glob('ext/**/*.{c,h,rb}') +
2222
[ 'COPYING', 'README.md' ]
2323
s.extensions << 'ext/extconf.rb'
2424
s.homepage = 'https://github.com/nilium/ruby-snowdata'
25-
s.license = 'Simplified BSD'
26-
s.has_rdoc = true
25+
s.license = 'BSD-2-Clause'
2726
s.extra_rdoc_files = [
2827
'ext/snow-data/snow-data.c',
2928
'README.md',
@@ -33,5 +32,5 @@ other structs as members.
3332
'--main' << 'README.md' <<
3433
'--markup=markdown' <<
3534
'--line-numbers'
36-
s.required_ruby_version = '>= 2.0.0'
35+
s.required_ruby_version = '>= 3.3.0'
3736
}

0 commit comments

Comments
 (0)