Refine examples.
This commit is contained in:
parent
d843baca12
commit
146129279c
@ -1,3 +1,6 @@
|
|||||||
|
#![cfg(not(feature = "no_index"))]
|
||||||
|
#![cfg(not(feature = "no_object"))]
|
||||||
|
|
||||||
use rhai::{Engine, EvalAltResult};
|
use rhai::{Engine, EvalAltResult};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -14,8 +17,6 @@ impl TestStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
@ -46,8 +47,3 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "no_index", feature = "no_object"))]
|
|
||||||
fn main() {
|
|
||||||
panic!("This example does not run under 'no_index' or 'no_object'.")
|
|
||||||
}
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![cfg(not(feature = "no_object"))]
|
||||||
|
|
||||||
use rhai::{Engine, EvalAltResult};
|
use rhai::{Engine, EvalAltResult};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -15,7 +17,6 @@ impl TestStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
@ -36,8 +37,3 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "no_object")]
|
|
||||||
fn main() {
|
|
||||||
panic!("This example does not run under 'no_object'.");
|
|
||||||
}
|
|
||||||
|
@ -1,19 +1,6 @@
|
|||||||
#[cfg(any(not(feature = "serde"), feature = "no_object"))]
|
#![cfg(feature = "serde")]
|
||||||
fn main() {
|
#![cfg(not(feature = "no_object"))]
|
||||||
println!("This example requires the 'serde' feature to run.");
|
|
||||||
println!("Try: cargo run --features serde --example serde");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fn main() {
|
|
||||||
example::ser();
|
|
||||||
println!();
|
|
||||||
example::de();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
mod example {
|
mod example {
|
||||||
use rhai::serde::{from_dynamic, to_dynamic};
|
use rhai::serde::{from_dynamic, to_dynamic};
|
||||||
use rhai::{Dynamic, Engine, Map};
|
use rhai::{Dynamic, Engine, Map};
|
||||||
@ -88,3 +75,9 @@ mod example {
|
|||||||
println!("Deserialized to struct: {:#?}", x);
|
println!("Deserialized to struct: {:#?}", x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
example::ser();
|
||||||
|
println!();
|
||||||
|
example::de();
|
||||||
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
///! This example registers a variety of functions that operate on strings.
|
///! This example registers a variety of functions that operate on strings.
|
||||||
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
|
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
|
||||||
|
#![cfg(not(feature = "no_object"))]
|
||||||
|
|
||||||
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
|
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
|
||||||
use std::io::{stdin, stdout, Write};
|
use std::io::{stdin, stdout, Write};
|
||||||
|
|
||||||
@ -64,10 +66,10 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
|||||||
engine.run_with_scope(
|
engine.run_with_scope(
|
||||||
&mut scope,
|
&mut scope,
|
||||||
r#"
|
r#"
|
||||||
display("Length", x.len());
|
display("Length", x.len);
|
||||||
x.trim();
|
x.trim();
|
||||||
display("Trimmed", x);
|
display("Trimmed", x);
|
||||||
display("Trimmed Length", x.len());
|
display("Trimmed Length", x.len);
|
||||||
display("Index of \"!!!\"", x.index_of("!!!"));
|
display("Index of \"!!!\"", x.index_of("!!!"));
|
||||||
"#,
|
"#,
|
||||||
)?;
|
)?;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Types to support chaining operations (i.e. indexing and dotting).
|
//! Types to support chaining operations (i.e. indexing and dotting).
|
||||||
#![cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
#![cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
|
|
||||||
use super::{calc_index, EvalState, GlobalRuntimeState, Target};
|
use super::{EvalState, GlobalRuntimeState, Target};
|
||||||
use crate::ast::{Expr, OpAssignment};
|
use crate::ast::{Expr, OpAssignment};
|
||||||
use crate::types::dynamic::Union;
|
use crate::types::dynamic::Union;
|
||||||
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR};
|
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR};
|
||||||
@ -790,7 +790,7 @@ impl Engine {
|
|||||||
.as_int()
|
.as_int()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
|
||||||
let len = arr.len();
|
let len = arr.len();
|
||||||
let arr_idx = calc_index(len, index, true, || {
|
let arr_idx = super::calc_index(len, index, true, || {
|
||||||
ERR::ErrorArrayBounds(len, index, pos).into()
|
ERR::ErrorArrayBounds(len, index, pos).into()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -804,7 +804,7 @@ impl Engine {
|
|||||||
.as_int()
|
.as_int()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
|
||||||
let len = arr.len();
|
let len = arr.len();
|
||||||
let arr_idx = calc_index(len, index, true, || {
|
let arr_idx = super::calc_index(len, index, true, || {
|
||||||
ERR::ErrorArrayBounds(len, index, pos).into()
|
ERR::ErrorArrayBounds(len, index, pos).into()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -845,10 +845,10 @@ impl Engine {
|
|||||||
let start = range.start;
|
let start = range.start;
|
||||||
let end = range.end;
|
let end = range.end;
|
||||||
|
|
||||||
let start = calc_index(BITS, start, false, || {
|
let start = super::calc_index(BITS, start, false, || {
|
||||||
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
|
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
|
||||||
})?;
|
})?;
|
||||||
let end = calc_index(BITS, end, false, || {
|
let end = super::calc_index(BITS, end, false, || {
|
||||||
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
|
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -870,10 +870,10 @@ impl Engine {
|
|||||||
let start = *range.start();
|
let start = *range.start();
|
||||||
let end = *range.end();
|
let end = *range.end();
|
||||||
|
|
||||||
let start = calc_index(BITS, start, false, || {
|
let start = super::calc_index(BITS, start, false, || {
|
||||||
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
|
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
|
||||||
})?;
|
})?;
|
||||||
let end = calc_index(BITS, end, false, || {
|
let end = super::calc_index(BITS, end, false, || {
|
||||||
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
|
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -914,7 +914,7 @@ impl Engine {
|
|||||||
|
|
||||||
const BITS: usize = std::mem::size_of::<crate::INT>() * 8;
|
const BITS: usize = std::mem::size_of::<crate::INT>() * 8;
|
||||||
|
|
||||||
let bit = calc_index(BITS, index, true, || {
|
let bit = super::calc_index(BITS, index, true, || {
|
||||||
ERR::ErrorBitFieldBounds(BITS, index, pos).into()
|
ERR::ErrorBitFieldBounds(BITS, index, pos).into()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Type to hold a mutable reference to the target of an evaluation.
|
//! Type to hold a mutable reference to the target of an evaluation.
|
||||||
|
|
||||||
use crate::types::dynamic::Variant;
|
use crate::types::dynamic::Variant;
|
||||||
use crate::{Dynamic, RhaiResultOf, INT};
|
use crate::{Dynamic, RhaiResultOf};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
@ -11,8 +11,9 @@ use std::prelude::v1::*;
|
|||||||
// Negative starting positions count from the end.
|
// Negative starting positions count from the end.
|
||||||
//
|
//
|
||||||
// Values going over bounds are limited to the actual length.
|
// Values going over bounds are limited to the actual length.
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) {
|
#[allow(dead_code)]
|
||||||
|
pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (usize, usize) {
|
||||||
let start = if start < 0 {
|
let start = if start < 0 {
|
||||||
start.checked_abs().map_or(0, |positive_start| {
|
start.checked_abs().map_or(0, |positive_start| {
|
||||||
length - usize::min(positive_start as usize, length)
|
length - usize::min(positive_start as usize, length)
|
||||||
@ -39,10 +40,11 @@ pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) {
|
|||||||
// Negative starting positions count from the end.
|
// Negative starting positions count from the end.
|
||||||
//
|
//
|
||||||
// Values going over bounds call the provided closure to return a default value or an error.
|
// Values going over bounds call the provided closure to return a default value or an error.
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn calc_index<E>(
|
pub fn calc_index<E>(
|
||||||
length: usize,
|
length: usize,
|
||||||
start: INT,
|
start: crate::INT,
|
||||||
negative_count_from_end: bool,
|
negative_count_from_end: bool,
|
||||||
err: impl Fn() -> Result<usize, E>,
|
err: impl Fn() -> Result<usize, E>,
|
||||||
) -> Result<usize, E> {
|
) -> Result<usize, E> {
|
||||||
|
Loading…
Reference in New Issue
Block a user