spo_rhai/api/deprecated.rs
1//! Module containing all deprecated API that will be removed in the next major version.
2
3use crate::func::{RhaiFunc, SendSync};
4use crate::types::dynamic::Variant;
5use crate::{
6 Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, FnPtr, FuncRegistration, Identifier,
7 ImmutableString, Module, NativeCallContext, Position, RhaiNativeFunc, RhaiResult, RhaiResultOf,
8 Scope, SharedModule, TypeBuilder, AST,
9};
10use std::any::TypeId;
11#[cfg(feature = "no_std")]
12use std::prelude::v1::*;
13
14#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
15use crate::func::register::Mut;
16
17#[cfg(not(feature = "no_std"))]
18#[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
19use std::path::PathBuf;
20
21impl Engine {
22 /// Evaluate a file, but throw away the result and only return error (if any).
23 /// Useful for when you don't need the result, but still need to keep track of possible errors.
24 ///
25 /// Not available under `no_std` or `WASM`.
26 ///
27 /// # Deprecated
28 ///
29 /// This method is deprecated.
30 /// Use [`run_file`][Engine::run_file] instead.
31 ///
32 /// This method will be removed in the next major version.
33 #[deprecated(since = "1.1.0", note = "use `run_file` instead")]
34 #[cfg(not(feature = "no_std"))]
35 #[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
36 #[inline(always)]
37 pub fn consume_file(&self, path: PathBuf) -> RhaiResultOf<()> {
38 self.run_file(path)
39 }
40
41 /// Evaluate a file with own scope, but throw away the result and only return error (if any).
42 /// Useful for when you don't need the result, but still need to keep track of possible errors.
43 ///
44 /// Not available under `no_std` or `WASM`.
45 ///
46 /// # Deprecated
47 ///
48 /// This method is deprecated.
49 /// Use [`run_file_with_scope`][Engine::run_file_with_scope] instead.
50 ///
51 /// This method will be removed in the next major version.
52 #[deprecated(since = "1.1.0", note = "use `run_file_with_scope` instead")]
53 #[cfg(not(feature = "no_std"))]
54 #[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
55 #[inline(always)]
56 pub fn consume_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
57 self.run_file_with_scope(scope, path)
58 }
59
60 /// Evaluate a string, but throw away the result and only return error (if any).
61 /// Useful for when you don't need the result, but still need to keep track of possible errors.
62 ///
63 /// # Deprecated
64 ///
65 /// This method is deprecated.
66 /// Use [`run`][Engine::run] instead.
67 ///
68 /// This method will be removed in the next major version.
69 #[deprecated(since = "1.1.0", note = "use `run` instead")]
70 #[inline(always)]
71 pub fn consume(&self, script: &str) -> RhaiResultOf<()> {
72 self.run(script)
73 }
74
75 /// Evaluate a string with own scope, but throw away the result and only return error (if any).
76 /// Useful for when you don't need the result, but still need to keep track of possible errors.
77 ///
78 /// # Deprecated
79 ///
80 /// This method is deprecated.
81 /// Use [`run_with_scope`][Engine::run_with_scope] instead.
82 ///
83 /// This method will be removed in the next major version.
84 #[deprecated(since = "1.1.0", note = "use `run_with_scope` instead")]
85 #[inline(always)]
86 pub fn consume_with_scope(&self, scope: &mut Scope, script: &str) -> RhaiResultOf<()> {
87 self.run_with_scope(scope, script)
88 }
89
90 /// Evaluate an [`AST`], but throw away the result and only return error (if any).
91 /// Useful for when you don't need the result, but still need to keep track of possible errors.
92 ///
93 /// # Deprecated
94 ///
95 /// This method is deprecated.
96 /// Use [`run_ast`][Engine::run_ast] instead.
97 ///
98 /// This method will be removed in the next major version.
99 #[deprecated(since = "1.1.0", note = "use `run_ast` instead")]
100 #[inline(always)]
101 pub fn consume_ast(&self, ast: &AST) -> RhaiResultOf<()> {
102 self.run_ast(ast)
103 }
104
105 /// Evaluate an [`AST`] with own scope, but throw away the result and only return error (if any).
106 /// Useful for when you don't need the result, but still need to keep track of possible errors.
107 ///
108 /// # Deprecated
109 ///
110 /// This method is deprecated.
111 /// Use [`run_ast_with_scope`][Engine::run_ast_with_scope] instead.
112 ///
113 /// This method will be removed in the next major version.
114 #[deprecated(since = "1.1.0", note = "use `run_ast_with_scope` instead")]
115 #[inline(always)]
116 pub fn consume_ast_with_scope(&self, scope: &mut Scope, ast: &AST) -> RhaiResultOf<()> {
117 self.run_ast_with_scope(scope, ast)
118 }
119 /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments
120 /// and optionally a value for binding to the `this` pointer.
121 ///
122 /// Not available under `no_function`.
123 ///
124 /// There is an option to evaluate the [`AST`] to load necessary modules before calling the function.
125 ///
126 /// # Deprecated
127 ///
128 /// This method is deprecated.
129 /// Use [`call_fn_with_options`][Engine::call_fn_with_options] instead.
130 ///
131 /// This method will be removed in the next major version.
132 #[deprecated(since = "1.1.0", note = "use `call_fn_with_options` instead")]
133 #[cfg(not(feature = "no_function"))]
134 #[inline(always)]
135 pub fn call_fn_dynamic(
136 &self,
137 scope: &mut Scope,
138 ast: &AST,
139 eval_ast: bool,
140 name: impl AsRef<str>,
141 this_ptr: Option<&mut Dynamic>,
142 arg_values: impl AsMut<[Dynamic]>,
143 ) -> RhaiResult {
144 #[allow(deprecated)]
145 self.call_fn_raw(scope, ast, eval_ast, true, name, this_ptr, arg_values)
146 }
147 /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments.
148 ///
149 /// Not available under `no_function`.
150 ///
151 /// # Deprecated
152 ///
153 /// This method is deprecated.
154 /// Use [`call_fn_with_options`][Engine::call_fn_with_options] instead.
155 ///
156 /// This method will be removed in the next major version.
157 #[deprecated(since = "1.12.0", note = "use `call_fn_with_options` instead")]
158 #[cfg(not(feature = "no_function"))]
159 #[inline(always)]
160 pub fn call_fn_raw(
161 &self,
162 scope: &mut Scope,
163 ast: &AST,
164 eval_ast: bool,
165 rewind_scope: bool,
166 name: impl AsRef<str>,
167 this_ptr: Option<&mut Dynamic>,
168 arg_values: impl AsMut<[Dynamic]>,
169 ) -> RhaiResult {
170 let mut arg_values = arg_values;
171
172 let options = crate::CallFnOptions {
173 this_ptr,
174 eval_ast,
175 rewind_scope,
176 ..<_>::default()
177 };
178
179 self._call_fn(
180 scope,
181 &mut crate::eval::GlobalRuntimeState::new(self),
182 &mut crate::eval::Caches::new(),
183 ast,
184 name.as_ref(),
185 arg_values.as_mut(),
186 options,
187 )
188 }
189 /// Register a custom fallible function with the [`Engine`].
190 ///
191 /// # Deprecated
192 ///
193 /// This method is deprecated.
194 /// Use [`register_fn`][Engine::register_fn] instead.
195 ///
196 /// This method will be removed in the next major version.
197 #[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
198 #[inline(always)]
199 pub fn register_result_fn<A: 'static, const N: usize, const X: bool, R: Variant + Clone>(
200 &mut self,
201 name: impl AsRef<str> + Into<Identifier>,
202 func: impl RhaiNativeFunc<A, N, X, R, true> + SendSync + 'static,
203 ) -> &mut Self {
204 self.register_fn(name, func)
205 }
206 /// Register a getter function for a member of a registered type with the [`Engine`].
207 ///
208 /// The function signature must start with `&mut self` and not `&self`.
209 ///
210 /// Not available under `no_object`.
211 ///
212 /// # Deprecated
213 ///
214 /// This method is deprecated.
215 /// Use [`register_get`][Engine::register_get] instead.
216 ///
217 /// This method will be removed in the next major version.
218 #[deprecated(since = "1.9.1", note = "use `register_get` instead")]
219 #[cfg(not(feature = "no_object"))]
220 #[inline(always)]
221 pub fn register_get_result<T: Variant + Clone, const X: bool, R: Variant + Clone>(
222 &mut self,
223 name: impl AsRef<str>,
224 get_fn: impl RhaiNativeFunc<(Mut<T>,), 1, X, R, true> + SendSync + 'static,
225 ) -> &mut Self {
226 self.register_get(name, get_fn)
227 }
228 /// Register a setter function for a member of a registered type with the [`Engine`].
229 ///
230 /// Not available under `no_object`.
231 ///
232 /// # Deprecated
233 ///
234 /// This method is deprecated.
235 /// Use [`register_set`][Engine::register_set] instead.
236 ///
237 /// This method will be removed in the next major version.
238 #[deprecated(since = "1.9.1", note = "use `register_set` instead")]
239 #[cfg(not(feature = "no_object"))]
240 #[inline(always)]
241 pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, const X: bool>(
242 &mut self,
243 name: impl AsRef<str>,
244 set_fn: impl RhaiNativeFunc<(Mut<T>, V), 2, X, (), true> + SendSync + 'static,
245 ) -> &mut Self {
246 self.register_set(name, set_fn)
247 }
248 /// Register an index getter for a custom type with the [`Engine`].
249 ///
250 /// The function signature must start with `&mut self` and not `&self`.
251 ///
252 /// Not available under both `no_index` and `no_object`.
253 ///
254 /// # Deprecated
255 ///
256 /// This method is deprecated.
257 /// Use [`register_indexer_get`][Engine::register_indexer_get] instead.
258 ///
259 /// This method will be removed in the next major version.
260 #[deprecated(since = "1.9.1", note = "use `register_indexer_get` instead")]
261 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
262 #[inline(always)]
263 pub fn register_indexer_get_result<
264 T: Variant + Clone,
265 IDX: Variant + Clone,
266 R: Variant + Clone,
267 const X: bool,
268 >(
269 &mut self,
270 get_fn: impl RhaiNativeFunc<(Mut<T>, IDX), 2, X, R, true> + SendSync + 'static,
271 ) -> &mut Self {
272 self.register_indexer_get(get_fn)
273 }
274 /// Register an index setter for a custom type with the [`Engine`].
275 ///
276 /// Not available under both `no_index` and `no_object`.
277 ///
278 /// # Deprecated
279 ///
280 /// This method is deprecated.
281 /// Use [`register_indexer_set`][Engine::register_indexer_set] instead.
282 ///
283 /// This method will be removed in the next major version.
284 #[deprecated(since = "1.9.1", note = "use `register_indexer_set` instead")]
285 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
286 #[inline(always)]
287 pub fn register_indexer_set_result<
288 T: Variant + Clone,
289 IDX: Variant + Clone,
290 R: Variant + Clone,
291 const X: bool,
292 >(
293 &mut self,
294 set_fn: impl RhaiNativeFunc<(Mut<T>, IDX, R), 3, X, (), true> + SendSync + 'static,
295 ) -> &mut Self {
296 self.register_indexer_set(set_fn)
297 }
298 /// Register a custom syntax with the [`Engine`].
299 ///
300 /// Not available under `no_custom_syntax`.
301 ///
302 /// # Deprecated
303 ///
304 /// This method is deprecated.
305 /// Use [`register_custom_syntax_with_state_raw`][Engine::register_custom_syntax_with_state_raw] instead.
306 ///
307 /// This method will be removed in the next major version.
308 #[deprecated(
309 since = "1.11.0",
310 note = "use `register_custom_syntax_with_state_raw` instead"
311 )]
312 #[inline(always)]
313 #[cfg(not(feature = "no_custom_syntax"))]
314 pub fn register_custom_syntax_raw(
315 &mut self,
316 key: impl Into<Identifier>,
317 parse: impl Fn(&[ImmutableString], &str) -> crate::parser::ParseResult<Option<ImmutableString>>
318 + SendSync
319 + 'static,
320 scope_may_be_changed: bool,
321 func: impl Fn(&mut crate::EvalContext, &[crate::Expression]) -> RhaiResult + SendSync + 'static,
322 ) -> &mut Self {
323 self.register_custom_syntax_with_state_raw(
324 key,
325 move |keywords, look_ahead, _| parse(keywords, look_ahead),
326 scope_may_be_changed,
327 move |context, expressions, _| func(context, expressions),
328 )
329 }
330 /// _(internals)_ Evaluate a list of statements with no `this` pointer.
331 /// Exported under the `internals` feature only.
332 ///
333 /// # Deprecated
334 ///
335 /// This method is deprecated. It will be removed in the next major version.
336 #[cfg(feature = "internals")]
337 #[inline(always)]
338 #[deprecated(since = "1.12.0")]
339 pub fn eval_statements_raw(
340 &self,
341 global: &mut crate::eval::GlobalRuntimeState,
342 caches: &mut crate::eval::Caches,
343 scope: &mut Scope,
344 statements: &[crate::ast::Stmt],
345 ) -> RhaiResult {
346 self.eval_global_statements(global, caches, scope, statements, true)
347 }
348}
349
350impl Dynamic {
351 /// Convert the [`Dynamic`] into a [`String`] and return it.
352 /// If there are other references to the same string, a cloned copy is returned.
353 /// Returns the name of the actual type if the cast fails.
354 ///
355 /// # Deprecated
356 ///
357 /// This method is deprecated.
358 /// Use [`into_string`][Dynamic::into_string] instead.
359 ///
360 /// This method will be removed in the next major version.
361 #[deprecated(since = "1.1.0", note = "use `into_string` instead")]
362 #[inline(always)]
363 pub fn as_string(self) -> Result<String, &'static str> {
364 self.into_string()
365 }
366
367 /// Convert the [`Dynamic`] into an [`ImmutableString`] and return it.
368 /// Returns the name of the actual type if the cast fails.
369 ///
370 /// # Deprecated
371 ///
372 /// This method is deprecated.
373 /// Use [`into_immutable_string`][Dynamic::into_immutable_string] instead.
374 ///
375 /// This method will be removed in the next major version.
376 #[deprecated(since = "1.1.0", note = "use `into_immutable_string` instead")]
377 #[inline(always)]
378 pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
379 self.into_immutable_string()
380 }
381}
382
383impl AST {
384 /// _(internals)_ Get the internal [`Module`][crate::Module] containing all script-defined functions.
385 /// Exported under the `internals` feature only.
386 ///
387 /// Not available under `no_function`.
388 ///
389 /// # Deprecated
390 ///
391 /// This method is deprecated. Use [`shared_lib`][AST::shared_lib] instead.
392 ///
393 /// This method will be removed in the next major version.
394 #[deprecated(since = "1.3.0", note = "use `shared_lib` instead")]
395 #[cfg(feature = "internals")]
396 #[cfg(not(feature = "no_function"))]
397 #[inline(always)]
398 #[must_use]
399 pub fn lib(&self) -> &crate::Module {
400 self.shared_lib()
401 }
402 /// Clear the documentation.
403 /// Exported under the `metadata` feature only.
404 ///
405 /// # Deprecated
406 ///
407 /// This method will be removed in the next major version.
408 #[deprecated(since = "1.17.0")]
409 #[cfg(feature = "metadata")]
410 #[inline(always)]
411 pub fn clear_doc(&mut self) -> &mut Self {
412 self.doc.clear();
413 self
414 }
415}
416
417impl NativeCallContext<'_> {
418 /// Create a new [`NativeCallContext`].
419 ///
420 /// # Unimplemented
421 ///
422 /// This method is deprecated. It is no longer implemented and always panics.
423 ///
424 /// Use [`FnPtr::call`] to call a function pointer directly.
425 ///
426 /// This method will be removed in the next major version.
427 #[deprecated(
428 since = "1.3.0",
429 note = "use `FnPtr::call` to call a function pointer directly."
430 )]
431 #[inline(always)]
432 #[must_use]
433 #[allow(unused_variables)]
434 pub fn new(engine: &Engine, fn_name: &str, lib: &[SharedModule]) -> Self {
435 unimplemented!("`NativeCallContext::new` is deprecated");
436 }
437
438 /// Call a function inside the call context.
439 ///
440 /// # Deprecated
441 ///
442 /// This method is deprecated.
443 /// Use [`call_fn_raw`][NativeCallContext::call_fn_raw] instead.
444 ///
445 /// This method will be removed in the next major version.
446 #[deprecated(since = "1.2.0", note = "use `call_fn_raw` instead")]
447 #[inline(always)]
448 pub fn call_fn_dynamic_raw(
449 &self,
450 fn_name: impl AsRef<str>,
451 is_method_call: bool,
452 args: &mut [&mut Dynamic],
453 ) -> RhaiResult {
454 self.call_fn_raw(fn_name.as_ref(), is_method_call, is_method_call, args)
455 }
456}
457
458#[allow(useless_deprecated)]
459#[deprecated(since = "1.2.0", note = "explicitly wrap `EvalAltResult` in `Err`")]
460impl<T> From<EvalAltResult> for RhaiResultOf<T> {
461 #[inline(always)]
462 fn from(err: EvalAltResult) -> Self {
463 Err(err.into())
464 }
465}
466
467impl FnPtr {
468 /// Get the number of curried arguments.
469 ///
470 /// # Deprecated
471 ///
472 /// This method is deprecated.
473 /// Use [`curry().len()`][`FnPtr::curry`] instead.
474 ///
475 /// This method will be removed in the next major version.
476 #[deprecated(since = "1.8.0", note = "use `curry().len()` instead")]
477 #[inline(always)]
478 #[must_use]
479 pub fn num_curried(&self) -> usize {
480 self.curry().len()
481 }
482 /// Call the function pointer with curried arguments (if any).
483 /// The function may be script-defined (not available under `no_function`) or native Rust.
484 ///
485 /// This method is intended for calling a function pointer that is passed into a native Rust
486 /// function as an argument. Therefore, the [`AST`] is _NOT_ evaluated before calling the
487 /// function.
488 ///
489 /// # Deprecated
490 ///
491 /// This method is deprecated.
492 /// Use [`call_within_context`][FnPtr::call_within_context] or [`call_raw`][FnPtr::call_raw] instead.
493 ///
494 /// This method will be removed in the next major version.
495 #[deprecated(
496 since = "1.3.0",
497 note = "use `call_within_context` or `call_raw` instead"
498 )]
499 #[inline(always)]
500 pub fn call_dynamic(
501 &self,
502 context: &NativeCallContext,
503 this_ptr: Option<&mut Dynamic>,
504 arg_values: impl AsMut<[Dynamic]>,
505 ) -> RhaiResult {
506 self.call_raw(context, this_ptr, arg_values)
507 }
508}
509
510#[cfg(not(feature = "no_custom_syntax"))]
511impl crate::Expression<'_> {
512 /// If this expression is a variable name, return it. Otherwise [`None`].
513 ///
514 /// # Deprecated
515 ///
516 /// This method is deprecated.
517 /// Use [`get_string_value`][crate::Expression::get_string_value] instead.
518 ///
519 /// This method will be removed in the next major version.
520 #[deprecated(since = "1.4.0", note = "use `get_string_value` instead")]
521 #[inline(always)]
522 #[must_use]
523 pub fn get_variable_name(&self) -> Option<&str> {
524 self.get_string_value()
525 }
526}
527
528impl Position {
529 /// Create a new [`Position`].
530 ///
531 /// If `line` is zero, then [`None`] is returned.
532 ///
533 /// If `position` is zero, then it is at the beginning of a line.
534 ///
535 /// # Deprecated
536 ///
537 /// This function is deprecated.
538 /// Use [`new`][Position::new] (which panics when `line` is zero) instead.
539 ///
540 /// This method will be removed in the next major version.
541 #[deprecated(since = "1.6.0", note = "use `new` instead")]
542 #[inline(always)]
543 #[must_use]
544 pub const fn new_const(line: u16, position: u16) -> Option<Self> {
545 if line == 0 {
546 None
547 } else {
548 Some(Self::new(line, position))
549 }
550 }
551}
552
553#[allow(deprecated)]
554impl<T: Variant + Clone> TypeBuilder<'_, T> {
555 /// Register a custom fallible function.
556 ///
557 /// # Deprecated
558 ///
559 /// This method is deprecated.
560 /// Use [`with_fn`][`TypeBuilder::with_fn`] instead.
561 ///
562 /// This method will be removed in the next major version.
563 #[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
564 #[inline(always)]
565 pub fn with_result_fn<S, A: 'static, const N: usize, const X: bool, R, FUNC>(
566 &mut self,
567 name: S,
568 method: FUNC,
569 ) -> &mut Self
570 where
571 S: AsRef<str> + Into<Identifier>,
572 R: Variant + Clone,
573 FUNC: RhaiNativeFunc<A, N, X, R, true> + SendSync + 'static,
574 {
575 self.with_fn(name, method)
576 }
577
578 /// Register a fallible getter function.
579 ///
580 /// The function signature must start with `&mut self` and not `&self`.
581 ///
582 /// Not available under `no_object`.
583 ///
584 /// # Deprecated
585 ///
586 /// This method is deprecated.
587 /// Use [`with_get`][`TypeBuilder::with_get`] instead.
588 ///
589 /// This method will be removed in the next major version.
590 #[deprecated(since = "1.9.1", note = "use `with_get` instead")]
591 #[cfg(not(feature = "no_object"))]
592 #[inline(always)]
593 pub fn with_get_result<const X: bool, R: Variant + Clone>(
594 &mut self,
595 name: impl AsRef<str>,
596 get_fn: impl RhaiNativeFunc<(Mut<T>,), 1, X, R, true> + SendSync + 'static,
597 ) -> &mut Self {
598 self.with_get(name, get_fn)
599 }
600
601 /// Register a fallible setter function.
602 ///
603 /// Not available under `no_object`.
604 ///
605 /// # Deprecated
606 ///
607 /// This method is deprecated.
608 /// Use [`with_set`][`TypeBuilder::with_set`] instead.
609 ///
610 /// This method will be removed in the next major version.
611 #[deprecated(since = "1.9.1", note = "use `with_set` instead")]
612 #[cfg(not(feature = "no_object"))]
613 #[inline(always)]
614 pub fn with_set_result<const X: bool, R: Variant + Clone>(
615 &mut self,
616 name: impl AsRef<str>,
617 set_fn: impl RhaiNativeFunc<(Mut<T>, R), 2, X, (), true> + SendSync + 'static,
618 ) -> &mut Self {
619 self.with_set(name, set_fn)
620 }
621
622 /// Register an fallible index getter.
623 ///
624 /// The function signature must start with `&mut self` and not `&self`.
625 ///
626 /// Not available under both `no_index` and `no_object`.
627 ///
628 /// # Deprecated
629 ///
630 /// This method is deprecated.
631 /// Use [`with_indexer_get`][`TypeBuilder::with_indexer_get`] instead.
632 ///
633 /// This method will be removed in the next major version.
634 #[deprecated(since = "1.9.1", note = "use `with_indexer_get` instead")]
635 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
636 #[inline(always)]
637 pub fn with_indexer_get_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
638 &mut self,
639 get_fn: impl RhaiNativeFunc<(Mut<T>, IDX), 2, X, R, true> + SendSync + 'static,
640 ) -> &mut Self {
641 self.with_indexer_get(get_fn)
642 }
643
644 /// Register an fallible index setter.
645 ///
646 /// Not available under both `no_index` and `no_object`.
647 ///
648 /// # Deprecated
649 ///
650 /// This method is deprecated.
651 /// Use [`with_indexer_set`][`TypeBuilder::with_indexer_set`] instead.
652 ///
653 /// This method will be removed in the next major version.
654 #[deprecated(since = "1.9.1", note = "use `with_indexer_set` instead")]
655 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
656 #[inline(always)]
657 pub fn with_indexer_set_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
658 &mut self,
659 set_fn: impl RhaiNativeFunc<(Mut<T>, IDX, R), 3, X, (), true> + SendSync + 'static,
660 ) -> &mut Self {
661 self.with_indexer_set(set_fn)
662 }
663}
664
665impl Module {
666 /// Create a new [`Module`] with a pre-sized capacity for functions.
667 ///
668 /// # Deprecated
669 ///
670 /// This method is deprecated.
671 /// Use [`new`][`Module::new`] instead.
672 ///
673 /// This method will be removed in the next major version.
674 #[inline(always)]
675 #[must_use]
676 #[deprecated(since = "1.12.0", note = "use `new` instead")]
677 pub const fn with_capacity(_capacity: usize) -> Self {
678 Self::new()
679 }
680
681 /// Get the display name of a registered custom type.
682 ///
683 /// # Deprecated
684 ///
685 /// This method is deprecated.
686 /// Use [`get_custom_type_display_by_name`][`Module::get_custom_type_display_by_name`] instead.
687 ///
688 /// This method will be removed in the next major version.
689 #[inline(always)]
690 #[must_use]
691 #[deprecated(
692 since = "1.16.0",
693 note = "use `get_custom_type_display_by_name` instead"
694 )]
695 pub fn get_custom_type(&self, type_name: &str) -> Option<&str> {
696 self.get_custom_type_display_by_name(type_name)
697 }
698
699 /// Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
700 ///
701 /// If there is an existing Rust function of the same hash, it is replaced.
702 ///
703 /// # Deprecated
704 ///
705 /// This method is deprecated.
706 /// Use the [`FuncRegistration`] API instead.
707 ///
708 /// This method will be removed in the next major version.
709 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
710 #[inline(always)]
711 pub fn set_fn(
712 &mut self,
713 name: impl Into<Identifier>,
714 namespace: FnNamespace,
715 _access: FnAccess,
716 arg_names: Option<&[&str]>,
717 arg_types: impl AsRef<[TypeId]>,
718 func: RhaiFunc,
719 ) -> u64 {
720 let _arg_names = arg_names;
721
722 let fx = FuncRegistration::new(name).with_namespace(namespace);
723
724 #[cfg(feature = "metadata")]
725 let fx = if let Some(arg_names) = _arg_names {
726 fx.with_params_info(arg_names)
727 } else {
728 fx
729 };
730
731 fx.set_into_module_raw(self, arg_types, func).hash
732 }
733
734 /// _(metadata)_ Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
735 /// Exported under the `metadata` feature only.
736 ///
737 /// If there is an existing Rust function of the same hash, it is replaced.
738 ///
739 /// # Deprecated
740 ///
741 /// This method is deprecated.
742 /// Use the [`FuncRegistration`] API instead.
743 ///
744 /// This method will be removed in the next major version.
745 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
746 #[cfg(feature = "metadata")]
747 #[inline(always)]
748 pub fn set_fn_with_comments<C: AsRef<str>>(
749 &mut self,
750 name: impl Into<Identifier>,
751 namespace: FnNamespace,
752 _access: FnAccess,
753 arg_names: Option<&[&str]>,
754 arg_types: impl AsRef<[TypeId]>,
755 comments: impl IntoIterator<Item = C>,
756 func: RhaiFunc,
757 ) -> u64 {
758 FuncRegistration::new(name)
759 .with_namespace(namespace)
760 .with_params_info(arg_names.unwrap_or(&[]))
761 .with_comments(comments)
762 .set_into_module_raw(self, arg_types, func)
763 .hash
764 }
765
766 /// _(metadata)_ Update the metadata (parameter names/types and return type) of a registered function.
767 /// Exported under the `metadata` feature only.
768 ///
769 /// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call.
770 ///
771 /// # Deprecated
772 ///
773 /// This method is deprecated.
774 /// Use [`update_fn_metadata_with_comments`][`Module::update_fn_metadata_with_comments`] instead.
775 ///
776 /// This method will be removed in the next major version.
777 #[deprecated(
778 since = "1.17.0",
779 note = "use `update_fn_metadata_with_comments` instead"
780 )]
781 #[cfg(feature = "metadata")]
782 #[inline(always)]
783 #[allow(deprecated)]
784 pub fn update_fn_metadata<S: Into<Identifier>>(
785 &mut self,
786 hash_fn: u64,
787 arg_names: impl IntoIterator<Item = S>,
788 ) -> &mut Self {
789 self.update_fn_metadata_with_comments(hash_fn, arg_names, [""; 0])
790 }
791
792 /// _(metadata)_ Generate signatures for all the non-private functions in the [`Module`].
793 /// Exported under the `metadata` feature only.
794 ///
795 /// # Deprecated
796 ///
797 /// This method is deprecated.
798 /// Use [`gen_fn_signatures_with_mapper`][`Module::gen_fn_signatures_with_mapper`] instead.
799 ///
800 /// This method will be removed in the next major version.
801 #[deprecated(since = "1.17.0", note = "use `gen_fn_signatures_with_mapper` instead")]
802 #[cfg(feature = "metadata")]
803 #[inline(always)]
804 pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
805 self.gen_fn_signatures_with_mapper(|s| s.into())
806 }
807}
808
809#[cfg(not(feature = "no_index"))]
810use crate::plugin::*;
811
812#[cfg(not(feature = "no_index"))]
813#[export_module]
814pub mod deprecated_array_functions {
815 use crate::packages::array_basic::array_functions::*;
816 use crate::{Array, INT};
817
818 /// Iterate through all the elements in the array, applying a function named by `mapper` to each
819 /// element in turn, and return the results as a new array.
820 ///
821 /// # Deprecated API
822 ///
823 /// This method is deprecated and will be removed from the next major version.
824 /// Use `array.map(Fn("fn_name"))` instead.
825 ///
826 /// # Function Parameters
827 ///
828 /// A function with the same name as the value of `mapper` must exist taking these parameters:
829 ///
830 /// * `element`: copy of array element
831 /// * `index` _(optional)_: current index in the array
832 ///
833 /// # Example
834 ///
835 /// ```rhai
836 /// fn square(x) { x * x }
837 ///
838 /// fn multiply(x, i) { x * i }
839 ///
840 /// let x = [1, 2, 3, 4, 5];
841 ///
842 /// let y = x.map("square");
843 ///
844 /// print(y); // prints "[1, 4, 9, 16, 25]"
845 ///
846 /// let y = x.map("multiply");
847 ///
848 /// print(y); // prints "[0, 2, 6, 12, 20]"
849 /// ```
850 #[rhai_fn(name = "map", return_raw)]
851 pub fn map_by_fn_name(
852 ctx: NativeCallContext,
853 array: &mut Array,
854 mapper: &str,
855 ) -> RhaiResultOf<Array> {
856 map(ctx, array, FnPtr::new(mapper)?)
857 }
858 /// Iterate through all the elements in the array, applying a function named by `filter` to each
859 /// element in turn, and return a copy of all elements (in order) that return `true` as a new array.
860 ///
861 /// # Deprecated API
862 ///
863 /// This method is deprecated and will be removed from the next major version.
864 /// Use `array.filter(Fn("fn_name"))` instead.
865 ///
866 /// # Function Parameters
867 ///
868 /// A function with the same name as the value of `filter` must exist taking these parameters:
869 ///
870 /// * `element`: copy of array element
871 /// * `index` _(optional)_: current index in the array
872 ///
873 /// # Example
874 ///
875 /// ```rhai
876 /// fn screen(x, i) { x * i >= 10 }
877 ///
878 /// let x = [1, 2, 3, 4, 5];
879 ///
880 /// let y = x.filter("is_odd");
881 ///
882 /// print(y); // prints "[1, 3, 5]"
883 ///
884 /// let y = x.filter("screen");
885 ///
886 /// print(y); // prints "[12, 20]"
887 /// ```
888 #[rhai_fn(name = "filter", return_raw)]
889 pub fn filter_by_fn_name(
890 ctx: NativeCallContext,
891 array: &mut Array,
892 filter_func: &str,
893 ) -> RhaiResultOf<Array> {
894 filter(ctx, array, FnPtr::new(filter_func)?)
895 }
896 /// Iterate through all the elements in the array, applying a function named by `filter` to each
897 /// element in turn, and return the index of the first element that returns `true`.
898 /// If no element returns `true`, `-1` is returned.
899 ///
900 /// # Deprecated API
901 ///
902 /// This method is deprecated and will be removed from the next major version.
903 /// Use `array.index_of(Fn("fn_name"))` instead.
904 ///
905 /// # Function Parameters
906 ///
907 /// A function with the same name as the value of `filter` must exist taking these parameters:
908 ///
909 /// * `element`: copy of array element
910 /// * `index` _(optional)_: current index in the array
911 ///
912 /// # Example
913 ///
914 /// ```rhai
915 /// fn is_special(x) { x > 3 }
916 ///
917 /// fn is_dumb(x) { x > 8 }
918 ///
919 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
920 ///
921 /// print(x.index_of("is_special")); // prints 3
922 ///
923 /// print(x.index_of("is_dumb")); // prints -1
924 /// ```
925 #[rhai_fn(name = "index_of", return_raw, pure)]
926 pub fn index_of_by_fn_name(
927 ctx: NativeCallContext,
928 array: &mut Array,
929 filter: &str,
930 ) -> RhaiResultOf<INT> {
931 index_of_filter(ctx, array, FnPtr::new(filter)?)
932 }
933 /// Iterate through all the elements in the array, starting from a particular `start` position,
934 /// applying a function named by `filter` to each element in turn, and return the index of the
935 /// first element that returns `true`. If no element returns `true`, `-1` is returned.
936 ///
937 /// * If `start` < 0, position counts from the end of the array (`-1` is the last element).
938 /// * If `start` < -length of array, position counts from the beginning of the array.
939 /// * If `start` ≥ length of array, `-1` is returned.
940 ///
941 /// # Deprecated API
942 ///
943 /// This method is deprecated and will be removed from the next major version.
944 /// Use `array.index_of(Fn("fn_name"), start)` instead.
945 ///
946 /// # Function Parameters
947 ///
948 /// A function with the same name as the value of `filter` must exist taking these parameters:
949 ///
950 /// * `element`: copy of array element
951 /// * `index` _(optional)_: current index in the array
952 ///
953 /// # Example
954 ///
955 /// ```rhai
956 /// fn plural(x) { x > 1 }
957 ///
958 /// fn singular(x) { x < 2 }
959 ///
960 /// fn screen(x, i) { x * i > 20 }
961 ///
962 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
963 ///
964 /// print(x.index_of("plural", 3)); // prints 5: 2 > 1
965 ///
966 /// print(x.index_of("singular", 9)); // prints -1: nothing < 2 past index 9
967 ///
968 /// print(x.index_of("plural", 15)); // prints -1: nothing found past end of array
969 ///
970 /// print(x.index_of("plural", -5)); // prints 9: -5 = start from index 8
971 ///
972 /// print(x.index_of("plural", -99)); // prints 1: -99 = start from beginning
973 ///
974 /// print(x.index_of("screen", 8)); // prints 10: 3 * 10 > 20
975 /// ```
976 #[rhai_fn(name = "index_of", return_raw, pure)]
977 pub fn index_of_by_fn_name_starting_from(
978 ctx: NativeCallContext,
979 array: &mut Array,
980 filter: &str,
981 start: INT,
982 ) -> RhaiResultOf<INT> {
983 index_of_filter_starting_from(ctx, array, FnPtr::new(filter)?, start)
984 }
985 /// Return `true` if any element in the array that returns `true` when applied a function named
986 /// by `filter`.
987 ///
988 /// # Deprecated API
989 ///
990 /// This method is deprecated and will be removed from the next major version.
991 /// Use `array.some(Fn("fn_name"))` instead.
992 ///
993 /// # Function Parameters
994 ///
995 /// A function with the same name as the value of `filter` must exist taking these parameters:
996 ///
997 /// * `element`: copy of array element
998 /// * `index` _(optional)_: current index in the array
999 ///
1000 /// # Example
1001 ///
1002 /// ```rhai
1003 /// fn large(x) { x > 3 }
1004 ///
1005 /// fn huge(x) { x > 10 }
1006 ///
1007 /// fn screen(x, i) { i > x }
1008 ///
1009 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1010 ///
1011 /// print(x.some("large")); // prints true
1012 ///
1013 /// print(x.some("huge")); // prints false
1014 ///
1015 /// print(x.some("screen")); // prints true
1016 /// ```
1017 #[rhai_fn(name = "some", return_raw, pure)]
1018 pub fn some_by_fn_name(
1019 ctx: NativeCallContext,
1020 array: &mut Array,
1021 filter: &str,
1022 ) -> RhaiResultOf<bool> {
1023 some(ctx, array, FnPtr::new(filter)?)
1024 }
1025 /// Return `true` if all elements in the array return `true` when applied a function named by `filter`.
1026 ///
1027 /// # Deprecated API
1028 ///
1029 /// This method is deprecated and will be removed from the next major version.
1030 /// Use `array.all(Fn("fn_name"))` instead.
1031 ///
1032 /// # Function Parameters
1033 ///
1034 /// A function with the same name as the value of `filter` must exist taking these parameters:
1035 ///
1036 /// * `element`: copy of array element
1037 /// * `index` _(optional)_: current index in the array
1038 ///
1039 /// # Example
1040 ///
1041 /// ```rhai
1042 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1043 ///
1044 /// print(x.all(|v| v > 3)); // prints false
1045 ///
1046 /// print(x.all(|v| v > 1)); // prints true
1047 ///
1048 /// print(x.all(|v, i| i > v)); // prints false
1049 /// ```
1050 #[rhai_fn(name = "all", return_raw, pure)]
1051 pub fn all_by_fn_name(
1052 ctx: NativeCallContext,
1053 array: &mut Array,
1054 filter: &str,
1055 ) -> RhaiResultOf<bool> {
1056 all(ctx, array, FnPtr::new(filter)?)
1057 }
1058 /// Remove duplicated _consecutive_ elements from the array that return `true` when applied a
1059 /// function named by `comparer`.
1060 ///
1061 /// # Deprecated API
1062 ///
1063 /// This method is deprecated and will be removed from the next major version.
1064 /// Use `array.dedup(Fn("fn_name"))` instead.
1065 ///
1066 /// No element is removed if the correct `comparer` function does not exist.
1067 ///
1068 /// # Function Parameters
1069 ///
1070 /// * `element1`: copy of the current array element to compare
1071 /// * `element2`: copy of the next array element to compare
1072 ///
1073 /// ## Return Value
1074 ///
1075 /// `true` if `element1 == element2`, otherwise `false`.
1076 ///
1077 /// # Example
1078 ///
1079 /// ```rhai
1080 /// fn declining(a, b) { a >= b }
1081 ///
1082 /// let x = [1, 2, 2, 2, 3, 1, 2, 3, 4, 3, 3, 2, 1];
1083 ///
1084 /// x.dedup("declining");
1085 ///
1086 /// print(x); // prints "[1, 2, 3, 4]"
1087 /// ```
1088 #[rhai_fn(name = "dedup", return_raw)]
1089 pub fn dedup_by_fn_name(
1090 ctx: NativeCallContext,
1091 array: &mut Array,
1092 comparer: &str,
1093 ) -> RhaiResultOf<()> {
1094 dedup_by_comparer(ctx, array, FnPtr::new(comparer)?);
1095 Ok(())
1096 }
1097 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1098 ///
1099 /// # Deprecated API
1100 ///
1101 /// This method is deprecated and will be removed from the next major version.
1102 /// Use `array.reduce(Fn("fn_name"))` instead.
1103 ///
1104 /// # Function Parameters
1105 ///
1106 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1107 ///
1108 /// * `result`: accumulated result, initially `()`
1109 /// * `element`: copy of array element
1110 /// * `index` _(optional)_: current index in the array
1111 ///
1112 /// # Example
1113 ///
1114 /// ```rhai
1115 /// fn process(r, x) {
1116 /// x + (r ?? 0)
1117 /// }
1118 /// fn process_extra(r, x, i) {
1119 /// x + i + (r ?? 0)
1120 /// }
1121 ///
1122 /// let x = [1, 2, 3, 4, 5];
1123 ///
1124 /// let y = x.reduce("process");
1125 ///
1126 /// print(y); // prints 15
1127 ///
1128 /// let y = x.reduce("process_extra");
1129 ///
1130 /// print(y); // prints 25
1131 /// ```
1132 #[rhai_fn(name = "reduce", return_raw, pure)]
1133 pub fn reduce_by_fn_name(
1134 ctx: NativeCallContext,
1135 array: &mut Array,
1136 reducer: &str,
1137 ) -> RhaiResult {
1138 reduce(ctx, array, FnPtr::new(reducer)?)
1139 }
1140 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1141 ///
1142 /// # Deprecated API
1143 ///
1144 /// This method is deprecated and will be removed from the next major version.
1145 /// Use `array.reduce(Fn("fn_name"), initial)` instead.
1146 ///
1147 /// # Function Parameters
1148 ///
1149 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1150 ///
1151 /// * `result`: accumulated result, starting with the value of `initial`
1152 /// * `element`: copy of array element
1153 /// * `index` _(optional)_: current index in the array
1154 ///
1155 /// # Example
1156 ///
1157 /// ```rhai
1158 /// fn process(r, x) { x + r }
1159 ///
1160 /// fn process_extra(r, x, i) { x + i + r }
1161 ///
1162 /// let x = [1, 2, 3, 4, 5];
1163 ///
1164 /// let y = x.reduce("process", 5);
1165 ///
1166 /// print(y); // prints 20
1167 ///
1168 /// let y = x.reduce("process_extra", 5);
1169 ///
1170 /// print(y); // prints 30
1171 /// ```
1172 #[rhai_fn(name = "reduce", return_raw, pure)]
1173 pub fn reduce_by_fn_name_with_initial(
1174 ctx: NativeCallContext,
1175 array: &mut Array,
1176 reducer: &str,
1177 initial: Dynamic,
1178 ) -> RhaiResult {
1179 reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1180 }
1181 /// Reduce an array by iterating through all elements, in _reverse_ order,
1182 /// while applying a function named by `reducer`.
1183 ///
1184 /// # Deprecated API
1185 ///
1186 /// This method is deprecated and will be removed from the next major version.
1187 /// Use `array.reduce_rev(Fn("fn_name"))` instead.
1188 ///
1189 /// # Function Parameters
1190 ///
1191 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1192 ///
1193 /// * `result`: accumulated result, initially `()`
1194 /// * `element`: copy of array element
1195 /// * `index` _(optional)_: current index in the array
1196 ///
1197 /// # Example
1198 ///
1199 /// ```rhai
1200 /// fn process(r, x) {
1201 /// x + (r ?? 0)
1202 /// }
1203 /// fn process_extra(r, x, i) {
1204 /// x + i + (r ?? 0)
1205 /// }
1206 ///
1207 /// let x = [1, 2, 3, 4, 5];
1208 ///
1209 /// let y = x.reduce_rev("process");
1210 ///
1211 /// print(y); // prints 15
1212 ///
1213 /// let y = x.reduce_rev("process_extra");
1214 ///
1215 /// print(y); // prints 25
1216 /// ```
1217 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1218 pub fn reduce_rev_by_fn_name(
1219 ctx: NativeCallContext,
1220 array: &mut Array,
1221 reducer: &str,
1222 ) -> RhaiResult {
1223 reduce_rev(ctx, array, FnPtr::new(reducer)?)
1224 }
1225 /// Reduce an array by iterating through all elements, in _reverse_ order,
1226 /// while applying a function named by `reducer`.
1227 ///
1228 /// # Deprecated API
1229 ///
1230 /// This method is deprecated and will be removed from the next major version.
1231 /// Use `array.reduce_rev(Fn("fn_name"), initial)` instead.
1232 ///
1233 /// # Function Parameters
1234 ///
1235 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1236 ///
1237 /// * `result`: accumulated result, starting with the value of `initial`
1238 /// * `element`: copy of array element
1239 /// * `index` _(optional)_: current index in the array
1240 ///
1241 /// # Example
1242 ///
1243 /// ```rhai
1244 /// fn process(r, x) { x + r }
1245 ///
1246 /// fn process_extra(r, x, i) { x + i + r }
1247 ///
1248 /// let x = [1, 2, 3, 4, 5];
1249 ///
1250 /// let y = x.reduce_rev("process", 5);
1251 ///
1252 /// print(y); // prints 20
1253 ///
1254 /// let y = x.reduce_rev("process_extra", 5);
1255 ///
1256 /// print(y); // prints 30
1257 /// ```
1258 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1259 pub fn reduce_rev_by_fn_name_with_initial(
1260 ctx: NativeCallContext,
1261 array: &mut Array,
1262 reducer: &str,
1263 initial: Dynamic,
1264 ) -> RhaiResult {
1265 reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1266 }
1267 /// Sort the array based on applying a function named by `comparer`.
1268 ///
1269 /// # Deprecated API
1270 ///
1271 /// This method is deprecated and will be removed from the next major version.
1272 /// Use `array.sort(Fn("fn_name"))` instead.
1273 ///
1274 /// # Function Parameters
1275 ///
1276 /// A function with the same name as the value of `comparer` must exist taking these parameters:
1277 ///
1278 /// * `element1`: copy of the current array element to compare
1279 /// * `element2`: copy of the next array element to compare
1280 ///
1281 /// ## Return Value
1282 ///
1283 /// * Any integer > 0 if `element1 > element2`
1284 /// * Zero if `element1 == element2`
1285 /// * Any integer < 0 if `element1 < element2`
1286 ///
1287 /// # Example
1288 ///
1289 /// ```rhai
1290 /// fn reverse(a, b) {
1291 /// if a > b {
1292 /// -1
1293 /// } else if a < b {
1294 /// 1
1295 /// } else {
1296 /// 0
1297 /// }
1298 /// }
1299 /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1300 ///
1301 /// x.sort("reverse");
1302 ///
1303 /// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1304 /// ```
1305 #[rhai_fn(name = "sort", return_raw)]
1306 pub fn sort_by_fn_name(
1307 ctx: NativeCallContext,
1308 array: &mut Array,
1309 comparer: &str,
1310 ) -> RhaiResultOf<()> {
1311 sort(ctx, array, FnPtr::new(comparer)?);
1312 Ok(())
1313 }
1314 /// Remove all elements in the array that returns `true` when applied a function named by `filter`
1315 /// and return them as a new array.
1316 ///
1317 /// # Deprecated API
1318 ///
1319 /// This method is deprecated and will be removed from the next major version.
1320 /// Use `array.drain(Fn("fn_name"))` instead.
1321 ///
1322 /// # Function Parameters
1323 ///
1324 /// A function with the same name as the value of `filter` must exist taking these parameters:
1325 ///
1326 /// * `element`: copy of array element
1327 /// * `index` _(optional)_: current index in the array
1328 ///
1329 /// # Example
1330 ///
1331 /// ```rhai
1332 /// fn small(x) { x < 3 }
1333 ///
1334 /// fn screen(x, i) { x + i > 5 }
1335 ///
1336 /// let x = [1, 2, 3, 4, 5];
1337 ///
1338 /// let y = x.drain("small");
1339 ///
1340 /// print(x); // prints "[3, 4, 5]"
1341 ///
1342 /// print(y); // prints "[1, 2]"
1343 ///
1344 /// let z = x.drain("screen");
1345 ///
1346 /// print(x); // prints "[3, 4]"
1347 ///
1348 /// print(z); // prints "[5]"
1349 /// ```
1350 #[rhai_fn(name = "drain", return_raw)]
1351 pub fn drain_by_fn_name(
1352 ctx: NativeCallContext,
1353 array: &mut Array,
1354 filter: &str,
1355 ) -> RhaiResultOf<Array> {
1356 drain(ctx, array, FnPtr::new(filter)?)
1357 }
1358 /// Remove all elements in the array that do not return `true` when applied a function named by
1359 /// `filter` and return them as a new array.
1360 ///
1361 /// # Deprecated API
1362 ///
1363 /// This method is deprecated and will be removed from the next major version.
1364 /// Use `array.retain(Fn("fn_name"))` instead.
1365 ///
1366 /// # Function Parameters
1367 ///
1368 /// A function with the same name as the value of `filter` must exist taking these parameters:
1369 ///
1370 /// * `element`: copy of array element
1371 /// * `index` _(optional)_: current index in the array
1372 ///
1373 /// # Example
1374 ///
1375 /// ```rhai
1376 /// fn large(x) { x >= 3 }
1377 ///
1378 /// fn screen(x, i) { x + i <= 5 }
1379 ///
1380 /// let x = [1, 2, 3, 4, 5];
1381 ///
1382 /// let y = x.retain("large");
1383 ///
1384 /// print(x); // prints "[3, 4, 5]"
1385 ///
1386 /// print(y); // prints "[1, 2]"
1387 ///
1388 /// let z = x.retain("screen");
1389 ///
1390 /// print(x); // prints "[3, 4]"
1391 ///
1392 /// print(z); // prints "[5]"
1393 /// ```
1394 #[rhai_fn(name = "retain", return_raw)]
1395 pub fn retain_by_fn_name(
1396 ctx: NativeCallContext,
1397 array: &mut Array,
1398 filter: &str,
1399 ) -> RhaiResultOf<Array> {
1400 retain(ctx, array, FnPtr::new(filter)?)
1401 }
1402}
1403
1404pub mod config {
1405 pub mod hashing {
1406 /// Set the hashing seed. This is used to hash functions etc.
1407 ///
1408 /// # Deprecated
1409 ///
1410 /// This method is deprecated.
1411 /// Use [`set_hashing_seed`][crate::config::hashing::set_hashing_seed] instead.
1412 ///
1413 /// This method will be removed in the next major version.
1414 #[deprecated(since = "1.17.0", note = "use `set_hashing_seed` instead")]
1415 #[inline(always)]
1416 pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>> {
1417 crate::config::hashing::set_hashing_seed(new_seed)
1418 }
1419
1420 /// Get the current hashing Seed.
1421 ///
1422 /// # Deprecated
1423 ///
1424 /// This method is deprecated.
1425 /// Use [`get_hashing_seed`][crate::config::hashing::get_hashing_seed] instead.
1426 ///
1427 /// This method will be removed in the next major version.
1428 #[deprecated(since = "1.17.0", note = "use `get_hashing_seed` instead")]
1429 #[inline]
1430 #[must_use]
1431 pub fn get_ahash_seed() -> &'static Option<[u64; 4]> {
1432 crate::config::hashing::get_hashing_seed()
1433 }
1434 }
1435}