feat: EvalState.require_int

(cherry picked from commit 890441adf963f1a33ba75889cc9735deeefbf51c)
This commit is contained in:
Robert Hensing 2024-04-09 13:13:52 +02:00
parent 4688ccbf95
commit fbbc7cb490
2 changed files with 27 additions and 1 deletions

View file

@ -1,4 +1,4 @@
use crate::value::{Value, ValueType};
use crate::value::{Int, Value, ValueType};
use anyhow::Context as _;
use anyhow::{bail, Result};
use lazy_static::lazy_static;
@ -115,6 +115,15 @@ impl EvalState {
let r = unsafe { raw::get_type(self.context.ptr(), value.raw_ptr()) };
Ok(ValueType::from_raw(r))
}
pub fn require_int(&self, v: &Value) -> Result<Int> {
let t = self.value_type(v).unwrap();
if t != ValueType::Int {
bail!("expected an int, but got a {:?}", t);
}
let i = unsafe { raw::get_int(self.context.ptr(), v.raw_ptr()) };
Ok(i)
}
/// Not exposed, because the caller must always explicitly handle the context or not accept one at all.
fn get_string(&self, value: &Value) -> Result<String> {
let mut r = result_string_init!();
@ -302,6 +311,21 @@ mod tests {
.unwrap();
}
#[test]
fn eval_state_value_int() {
gc_registering_current_thread(|| {
let store = Store::open("auto").unwrap();
let es = EvalState::new(store).unwrap();
let v = es.eval_from_string("1", "<test>").unwrap();
es.force(&v).unwrap();
let t = es.value_type(&v).unwrap();
assert!(t == ValueType::Int);
let i = es.require_int(&v).unwrap();
assert!(i == 1);
})
.unwrap();
}
#[test]
fn eval_state_value_string() {
gc_registering_current_thread(|| {

View file

@ -4,6 +4,8 @@ use std::ptr::{null_mut, NonNull};
// TODO: test: cloning a thunk does not duplicate the evaluation.
pub type Int = i64;
/** The type of a value (or thunk) */
#[derive(Eq, PartialEq, Debug)]
pub enum ValueType {