-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ba2643
commit 0cbaf98
Showing
21 changed files
with
1,264 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use savvy::altrep::{ | ||
register_altinteger_class, register_altlogical_class, register_altreal_class, | ||
register_altstring_class, AltInteger, AltLogical, AltReal, AltString, | ||
}; | ||
use savvy::savvy; | ||
|
||
// integer | ||
|
||
struct MyAltInt(Vec<i32>); | ||
impl savvy::IntoExtPtrSexp for MyAltInt {} | ||
|
||
impl MyAltInt { | ||
fn new(x: Vec<i32>) -> Self { | ||
Self(x) | ||
} | ||
} | ||
|
||
impl AltInteger for MyAltInt { | ||
const CLASS_NAME: &'static str = "MyAltInt"; | ||
const PACKAGE_NAME: &'static str = "TestPackage"; | ||
|
||
fn length(&mut self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
fn elt(&mut self, i: usize) -> i32 { | ||
self.0[i] | ||
} | ||
} | ||
|
||
#[savvy] | ||
fn altint() -> savvy::Result<savvy::Sexp> { | ||
let v = MyAltInt::new(vec![1, 2, 3]); | ||
let v_altrep = v.into_altrep()?; | ||
Ok(savvy::Sexp(v_altrep)) | ||
} | ||
|
||
// real | ||
|
||
struct MyAltReal(Vec<f64>); | ||
impl savvy::IntoExtPtrSexp for MyAltReal {} | ||
|
||
impl MyAltReal { | ||
fn new(x: Vec<f64>) -> Self { | ||
Self(x) | ||
} | ||
} | ||
|
||
impl AltReal for MyAltReal { | ||
const CLASS_NAME: &'static str = "MyAltReal"; | ||
const PACKAGE_NAME: &'static str = "TestPackage"; | ||
|
||
fn length(&mut self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
fn elt(&mut self, i: usize) -> f64 { | ||
self.0[i] | ||
} | ||
} | ||
|
||
#[savvy] | ||
fn altreal() -> savvy::Result<savvy::Sexp> { | ||
let v = MyAltReal::new(vec![1.0, 2.0, 3.0]); | ||
let v_altrep = v.into_altrep()?; | ||
Ok(savvy::Sexp(v_altrep)) | ||
} | ||
|
||
// logical | ||
|
||
struct MyAltLogical(Vec<bool>); | ||
impl savvy::IntoExtPtrSexp for MyAltLogical {} | ||
|
||
impl MyAltLogical { | ||
fn new(x: Vec<bool>) -> Self { | ||
Self(x) | ||
} | ||
} | ||
|
||
impl AltLogical for MyAltLogical { | ||
const CLASS_NAME: &'static str = "MyAltLogical"; | ||
const PACKAGE_NAME: &'static str = "TestPackage"; | ||
|
||
fn length(&mut self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
fn elt(&mut self, i: usize) -> bool { | ||
self.0[i] | ||
} | ||
} | ||
|
||
#[savvy] | ||
fn altlogical() -> savvy::Result<savvy::Sexp> { | ||
let v = MyAltLogical::new(vec![true, false, true]); | ||
let v_altrep = v.into_altrep()?; | ||
Ok(savvy::Sexp(v_altrep)) | ||
} | ||
|
||
// string | ||
|
||
struct MyAltString(Vec<String>); | ||
impl savvy::IntoExtPtrSexp for MyAltString {} | ||
|
||
impl MyAltString { | ||
fn new(x: Vec<String>) -> Self { | ||
Self(x) | ||
} | ||
} | ||
|
||
impl AltString for MyAltString { | ||
const CLASS_NAME: &'static str = "MyAltString"; | ||
const PACKAGE_NAME: &'static str = "TestPackage"; | ||
|
||
fn length(&mut self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
fn elt(&mut self, i: usize) -> &str { | ||
self.0[i].as_str() | ||
} | ||
} | ||
|
||
#[savvy] | ||
fn altstring() -> savvy::Result<savvy::Sexp> { | ||
let v = MyAltString::new(vec!["1".to_string(), "2".to_string(), "3".to_string()]); | ||
let v_altrep = v.into_altrep()?; | ||
Ok(savvy::Sexp(v_altrep)) | ||
} | ||
|
||
// initialization | ||
|
||
#[savvy] | ||
fn init_altrep_class(dll_info: *mut savvy::ffi::DllInfo) -> savvy::Result<()> { | ||
register_altinteger_class::<MyAltInt>(dll_info)?; | ||
register_altreal_class::<MyAltReal>(dll_info)?; | ||
register_altlogical_class::<MyAltLogical>(dll_info)?; | ||
register_altstring_class::<MyAltString>(dll_info)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
test_that("altinteger works", { | ||
x <- altint() | ||
expect_equal(x[1], 1L) # ELT method | ||
expect_equal(length(x), 3L) # length method | ||
expect_equal(as.character(x), c("1", "2", "3")) # coerce method | ||
# duplicate method? dataptr method? I'm not sure | ||
x[1] <- 2L | ||
expect_equal(x, c(2L, 2L, 3L)) | ||
}) | ||
|
||
test_that("altreal works", { | ||
x <- altreal() | ||
expect_equal(x[1], 1) # ELT method | ||
expect_equal(length(x), 3L) # length method | ||
expect_equal(as.character(x), c("1", "2", "3")) # coerce method | ||
# duplicate method? dataptr method? I'm not sure | ||
x[1] <- 2 | ||
expect_equal(x, c(2, 2, 3)) | ||
}) | ||
|
||
test_that("altlogical works", { | ||
x <- altlogical() | ||
expect_equal(x[1], TRUE) # ELT method | ||
expect_equal(length(x), 3L) # length method | ||
expect_equal(as.character(x), c("TRUE", "FALSE", "TRUE")) # coerce method | ||
# duplicate method? dataptr method? I'm not sure | ||
x[1] <- FALSE | ||
expect_equal(x, c(FALSE, FALSE, TRUE)) | ||
}) | ||
|
||
test_that("altstring works", { | ||
x <- altstring() | ||
expect_equal(x[1], "1") # ELT method | ||
expect_equal(length(x), 3L) # length method | ||
expect_equal(as.integer(x), c(1L, 2L, 3L)) # coerce method | ||
# duplicate method? dataptr method? I'm not sure | ||
x[1] <- "foo" | ||
expect_equal(x, c("foo", "2", "3")) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.