M
Modular3w ago
Ryan

How Unsafe/Safe is this code?

I needed a way to store lists of data of different types in a list, so I have created a list like type that can store any type that conforms to CollectionElement. It works well and doesn't have any memory errors, but I wanted to get a second opinion on if it seems safe enough to use. One issue it has is that I can't check the type when you append or get from it since it does not store type info. Currently I just store the types size and compare to that.
# simple example:
var table = List[Column](Column(), Column(), Column())
table[0].append[Vector2](Vector2(0, 9))
table[1].append[Int64](10)
table[2].append[Float64](3.14)

var val = table[1].get[Int64](idx=0)
table[2].get[Float64](idx=0) *= 2
# simple example:
var table = List[Column](Column(), Column(), Column())
table[0].append[Vector2](Vector2(0, 9))
table[1].append[Int64](10)
table[2].append[Float64](3.14)

var val = table[1].get[Int64](idx=0)
table[2].get[Float64](idx=0) *= 2
The code was a bit too long to fit in one message, so here is a link to it. https://gist.github.com/RyanLeber/d9ca608a3334482283c81771ff106485
Gist
Column
Column. GitHub Gist: instantly share code, notes, and snippets.
3 Replies
sora
sora3w ago
Don't you need some sort of index to size mapping for this to work?
Ryan
RyanOP3w ago
Whenever you append or get from it, I just bitcasts the pointer to the provided type. So I don't need to keep track of element locations in the pointer because the pointer is accessed like it is UnsafePointer[T]. Is this what you are referencing?
fn append[T: CollectionElement](inout self, owned value: T) raises:
var data_ptr = self.data.bitcast[T]()
(data_ptr + self.element_count).init_pointee_move(value^)

fn get[T: CollectionElement](inout self, idx: Int) raises -> ref [__lifetime_of(self)] T:
return self.data.bitcast[T]()[idx]
fn append[T: CollectionElement](inout self, owned value: T) raises:
var data_ptr = self.data.bitcast[T]()
(data_ptr + self.element_count).init_pointee_move(value^)

fn get[T: CollectionElement](inout self, idx: Int) raises -> ref [__lifetime_of(self)] T:
return self.data.bitcast[T]()[idx]
Darkmatter
Darkmatter3w ago
I'm pretty sure this breaks the pointer provenance rules, so this is probably a lot of UB.
Want results from more Discord servers?
Add your server