yutannihilation / savvy

A simple R extension interface using Rust
https://yutannihilation.github.io/savvy/guide/
MIT License
72 stars 4 forks source link

Restore struct from ALTREP #223

Closed yutannihilation closed 6 months ago

yutannihilation commented 6 months ago

Close https://github.com/yutannihilation/savvy/issues/224 (as a side effect)

x <- altint()

restore_altint(x)
#> MyAltInt([1, 2, 3])

restore_altint(1L)
#> Error: Not an ALTREP
yutannihilation commented 6 months ago

Current behavior is super wierd for mutable semantics. This is because AltInteger trait materializes on the first duplication and return it afterwards. But, elt() looks at the original.

x <- savvyExamples:::altint()
x
#> [1] 1 2 3

# ...??
savvyExamples:::double_altint(x)
x
#> [1] 1 2 3

# ...???
x2 <- savvyExamples:::altint()
savvyExamples:::double_altint(x2)
x2
#> [1] 2 4 6

savvyExamples:::double_altint(x2)
x2
#> [1] 2 4 6

x2[1]
#> [1] 4

Created on 2024-04-30 with reprex v2.1.0

yutannihilation commented 6 months ago

Okay. Even if I implement Dataptr or Set_elt, copy-on-modify rules. I'm giving up.

x <- savvyExamples:::altint_mutable()
x
#> [1] 1 2 3

.Internal(inspect(x))
#> @0x000001e99ff2ae30 13 INTSXP g0c0 [REF(5)] (MyAltIntMutable)

x[1L] <- 2L

.Internal(inspect(x))
#> @0x000001e9a210e968 13 INTSXP g0c2 [REF(1)] (len=3, tl=0) 2,2,3

Created on 2024-05-02 with reprex v2.1.0