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 options,
181 scope,
182 ast,
183 name.as_ref(),
184 arg_values.as_mut(),
185 &mut self.new_global_runtime_state(),
186 &mut crate::eval::Caches::new(),
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 /// Source of the current function.
439 ///
440 /// # Deprecated
441 ///
442 /// This method is deprecated.
443 /// Use [`fn_source`][NativeCallContext::fn_source] instead.
444 ///
445 /// To get the source of the caller, use [`call_source`][NativeCallContext::call_source].
446 ///
447 /// This method will be removed in the next major version.
448 #[inline(always)]
449 #[must_use]
450 #[deprecated(
451 since = "1.22.0",
452 note = "use `fn_source` instead, or `call_source` for the source of the caller."
453 )]
454 pub const fn source(&self) -> Option<&str> {
455 self.fn_source()
456 }
457
458 /// [Position] of the function call in the caller.
459 ///
460 /// # Deprecated
461 ///
462 /// This method is deprecated.
463 /// Use [`call_position`][NativeCallContext::call_position] instead.
464 ///
465 /// This method will be removed in the next major version.
466 #[inline(always)]
467 #[must_use]
468 #[deprecated(since = "1.22.0", note = "use `call_position` instead.")]
469 pub const fn position(&self) -> Position {
470 self.call_position()
471 }
472
473 /// Call a function inside the call context.
474 ///
475 /// # Deprecated
476 ///
477 /// This method is deprecated.
478 /// Use [`call_fn_raw`][NativeCallContext::call_fn_raw] instead.
479 ///
480 /// This method will be removed in the next major version.
481 #[deprecated(since = "1.2.0", note = "use `call_fn_raw` instead")]
482 #[inline(always)]
483 pub fn call_fn_dynamic_raw(
484 &self,
485 fn_name: impl AsRef<str>,
486 is_method_call: bool,
487 args: &mut [&mut Dynamic],
488 ) -> RhaiResult {
489 self.call_fn_raw(fn_name.as_ref(), is_method_call, is_method_call, args)
490 }
491}
492
493#[allow(useless_deprecated)]
494#[deprecated(since = "1.2.0", note = "explicitly wrap `EvalAltResult` in `Err`")]
495impl<T> From<EvalAltResult> for RhaiResultOf<T> {
496 #[inline(always)]
497 fn from(err: EvalAltResult) -> Self {
498 Err(err.into())
499 }
500}
501
502impl FnPtr {
503 /// Get the number of curried arguments.
504 ///
505 /// # Deprecated
506 ///
507 /// This method is deprecated.
508 /// Use [`curry().len()`][`FnPtr::curry`] instead.
509 ///
510 /// This method will be removed in the next major version.
511 #[deprecated(since = "1.8.0", note = "use `curry().len()` instead")]
512 #[inline(always)]
513 #[must_use]
514 pub fn num_curried(&self) -> usize {
515 self.curry().len()
516 }
517 /// Call the function pointer with curried arguments (if any).
518 /// The function may be script-defined (not available under `no_function`) or native Rust.
519 ///
520 /// This method is intended for calling a function pointer that is passed into a native Rust
521 /// function as an argument. Therefore, the [`AST`] is _NOT_ evaluated before calling the
522 /// function.
523 ///
524 /// # Deprecated
525 ///
526 /// This method is deprecated.
527 /// Use [`call_within_context`][FnPtr::call_within_context] or [`call_raw`][FnPtr::call_raw] instead.
528 ///
529 /// This method will be removed in the next major version.
530 #[deprecated(
531 since = "1.3.0",
532 note = "use `call_within_context` or `call_raw` instead"
533 )]
534 #[inline(always)]
535 pub fn call_dynamic(
536 &self,
537 context: &NativeCallContext,
538 this_ptr: Option<&mut Dynamic>,
539 arg_values: impl AsMut<[Dynamic]>,
540 ) -> RhaiResult {
541 self.call_raw(context, this_ptr, arg_values)
542 }
543}
544
545#[cfg(not(feature = "no_custom_syntax"))]
546impl crate::Expression<'_> {
547 /// If this expression is a variable name, return it. Otherwise [`None`].
548 ///
549 /// # Deprecated
550 ///
551 /// This method is deprecated.
552 /// Use [`get_string_value`][crate::Expression::get_string_value] instead.
553 ///
554 /// This method will be removed in the next major version.
555 #[deprecated(since = "1.4.0", note = "use `get_string_value` instead")]
556 #[inline(always)]
557 #[must_use]
558 pub fn get_variable_name(&self) -> Option<&str> {
559 self.get_string_value()
560 }
561}
562
563impl Position {
564 /// Create a new [`Position`].
565 ///
566 /// If `line` is zero, then [`None`] is returned.
567 ///
568 /// If `position` is zero, then it is at the beginning of a line.
569 ///
570 /// # Deprecated
571 ///
572 /// This function is deprecated.
573 /// Use [`new`][Position::new] (which panics when `line` is zero) instead.
574 ///
575 /// This method will be removed in the next major version.
576 #[deprecated(since = "1.6.0", note = "use `new` instead")]
577 #[inline(always)]
578 #[must_use]
579 pub const fn new_const(line: u16, position: u16) -> Option<Self> {
580 if line == 0 {
581 None
582 } else {
583 Some(Self::new(line, position))
584 }
585 }
586}
587
588#[allow(deprecated)]
589impl<T: Variant + Clone> TypeBuilder<'_, T> {
590 /// Register a custom fallible function.
591 ///
592 /// # Deprecated
593 ///
594 /// This method is deprecated.
595 /// Use [`with_fn`][`TypeBuilder::with_fn`] instead.
596 ///
597 /// This method will be removed in the next major version.
598 #[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
599 #[inline(always)]
600 pub fn with_result_fn<S, A: 'static, const N: usize, const X: bool, R, FUNC>(
601 &mut self,
602 name: S,
603 method: FUNC,
604 ) -> &mut Self
605 where
606 S: AsRef<str> + Into<Identifier>,
607 R: Variant + Clone,
608 FUNC: RhaiNativeFunc<A, N, X, R, true> + SendSync + 'static,
609 {
610 self.with_fn(name, method)
611 }
612
613 /// Register a fallible getter function.
614 ///
615 /// The function signature must start with `&mut self` and not `&self`.
616 ///
617 /// Not available under `no_object`.
618 ///
619 /// # Deprecated
620 ///
621 /// This method is deprecated.
622 /// Use [`with_get`][`TypeBuilder::with_get`] instead.
623 ///
624 /// This method will be removed in the next major version.
625 #[deprecated(since = "1.9.1", note = "use `with_get` instead")]
626 #[cfg(not(feature = "no_object"))]
627 #[inline(always)]
628 pub fn with_get_result<const X: bool, R: Variant + Clone>(
629 &mut self,
630 name: impl AsRef<str>,
631 get_fn: impl RhaiNativeFunc<(Mut<T>,), 1, X, R, true> + SendSync + 'static,
632 ) -> &mut Self {
633 self.with_get(name, get_fn)
634 }
635
636 /// Register a fallible setter function.
637 ///
638 /// Not available under `no_object`.
639 ///
640 /// # Deprecated
641 ///
642 /// This method is deprecated.
643 /// Use [`with_set`][`TypeBuilder::with_set`] instead.
644 ///
645 /// This method will be removed in the next major version.
646 #[deprecated(since = "1.9.1", note = "use `with_set` instead")]
647 #[cfg(not(feature = "no_object"))]
648 #[inline(always)]
649 pub fn with_set_result<const X: bool, R: Variant + Clone>(
650 &mut self,
651 name: impl AsRef<str>,
652 set_fn: impl RhaiNativeFunc<(Mut<T>, R), 2, X, (), true> + SendSync + 'static,
653 ) -> &mut Self {
654 self.with_set(name, set_fn)
655 }
656
657 /// Register an fallible index getter.
658 ///
659 /// The function signature must start with `&mut self` and not `&self`.
660 ///
661 /// Not available under both `no_index` and `no_object`.
662 ///
663 /// # Deprecated
664 ///
665 /// This method is deprecated.
666 /// Use [`with_indexer_get`][`TypeBuilder::with_indexer_get`] instead.
667 ///
668 /// This method will be removed in the next major version.
669 #[deprecated(since = "1.9.1", note = "use `with_indexer_get` instead")]
670 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
671 #[inline(always)]
672 pub fn with_indexer_get_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
673 &mut self,
674 get_fn: impl RhaiNativeFunc<(Mut<T>, IDX), 2, X, R, true> + SendSync + 'static,
675 ) -> &mut Self {
676 self.with_indexer_get(get_fn)
677 }
678
679 /// Register an fallible index setter.
680 ///
681 /// Not available under both `no_index` and `no_object`.
682 ///
683 /// # Deprecated
684 ///
685 /// This method is deprecated.
686 /// Use [`with_indexer_set`][`TypeBuilder::with_indexer_set`] instead.
687 ///
688 /// This method will be removed in the next major version.
689 #[deprecated(since = "1.9.1", note = "use `with_indexer_set` instead")]
690 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
691 #[inline(always)]
692 pub fn with_indexer_set_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
693 &mut self,
694 set_fn: impl RhaiNativeFunc<(Mut<T>, IDX, R), 3, X, (), true> + SendSync + 'static,
695 ) -> &mut Self {
696 self.with_indexer_set(set_fn)
697 }
698}
699
700impl Module {
701 /// Create a new [`Module`] with a pre-sized capacity for functions.
702 ///
703 /// # Deprecated
704 ///
705 /// This method is deprecated.
706 /// Use [`new`][`Module::new`] instead.
707 ///
708 /// This method will be removed in the next major version.
709 #[inline(always)]
710 #[must_use]
711 #[deprecated(since = "1.12.0", note = "use `new` instead")]
712 pub const fn with_capacity(_capacity: usize) -> Self {
713 Self::new()
714 }
715
716 /// Get the display name of a registered custom type.
717 ///
718 /// # Deprecated
719 ///
720 /// This method is deprecated.
721 /// Use [`get_custom_type_display_by_name`][`Module::get_custom_type_display_by_name`] instead.
722 ///
723 /// This method will be removed in the next major version.
724 #[inline(always)]
725 #[must_use]
726 #[deprecated(
727 since = "1.16.0",
728 note = "use `get_custom_type_display_by_name` instead"
729 )]
730 pub fn get_custom_type(&self, type_name: &str) -> Option<&str> {
731 self.get_custom_type_display_by_name(type_name)
732 }
733
734 /// Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
735 ///
736 /// If there is an existing Rust function of the same hash, it is replaced.
737 ///
738 /// # Deprecated
739 ///
740 /// This method is deprecated.
741 /// Use the [`FuncRegistration`] API instead.
742 ///
743 /// This method will be removed in the next major version.
744 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
745 #[inline(always)]
746 pub fn set_fn(
747 &mut self,
748 name: impl Into<Identifier>,
749 namespace: FnNamespace,
750 _access: FnAccess,
751 arg_names: Option<&[&str]>,
752 arg_types: impl AsRef<[TypeId]>,
753 func: RhaiFunc,
754 ) -> u64 {
755 let _arg_names = arg_names;
756
757 let fx = FuncRegistration::new(name).with_namespace(namespace);
758
759 #[cfg(feature = "metadata")]
760 let fx = if let Some(arg_names) = _arg_names {
761 fx.with_params_info(arg_names)
762 } else {
763 fx
764 };
765
766 fx.set_into_module_raw(self, arg_types, func).hash
767 }
768
769 /// _(metadata)_ Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
770 /// Exported under the `metadata` feature only.
771 ///
772 /// If there is an existing Rust function of the same hash, it is replaced.
773 ///
774 /// # Deprecated
775 ///
776 /// This method is deprecated.
777 /// Use the [`FuncRegistration`] API instead.
778 ///
779 /// This method will be removed in the next major version.
780 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
781 #[cfg(feature = "metadata")]
782 #[inline(always)]
783 pub fn set_fn_with_comments<C: AsRef<str>>(
784 &mut self,
785 name: impl Into<Identifier>,
786 namespace: FnNamespace,
787 _access: FnAccess,
788 arg_names: Option<&[&str]>,
789 arg_types: impl AsRef<[TypeId]>,
790 comments: impl IntoIterator<Item = C>,
791 func: RhaiFunc,
792 ) -> u64 {
793 FuncRegistration::new(name)
794 .with_namespace(namespace)
795 .with_params_info(arg_names.unwrap_or(&[]))
796 .with_comments(comments)
797 .set_into_module_raw(self, arg_types, func)
798 .hash
799 }
800
801 /// _(metadata)_ Update the metadata (parameter names/types and return type) of a registered function.
802 /// Exported under the `metadata` feature only.
803 ///
804 /// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call.
805 ///
806 /// # Deprecated
807 ///
808 /// This method is deprecated.
809 /// Use [`update_fn_metadata_with_comments`][`Module::update_fn_metadata_with_comments`] instead.
810 ///
811 /// This method will be removed in the next major version.
812 #[deprecated(
813 since = "1.17.0",
814 note = "use `update_fn_metadata_with_comments` instead"
815 )]
816 #[cfg(feature = "metadata")]
817 #[inline(always)]
818 #[allow(deprecated)]
819 pub fn update_fn_metadata<S: Into<Identifier>>(
820 &mut self,
821 hash_fn: u64,
822 arg_names: impl IntoIterator<Item = S>,
823 ) -> &mut Self {
824 self.update_fn_metadata_with_comments(hash_fn, arg_names, [""; 0])
825 }
826
827 /// _(metadata)_ Generate signatures for all the non-private functions in the [`Module`].
828 /// Exported under the `metadata` feature only.
829 ///
830 /// # Deprecated
831 ///
832 /// This method is deprecated.
833 /// Use [`gen_fn_signatures_with_mapper`][`Module::gen_fn_signatures_with_mapper`] instead.
834 ///
835 /// This method will be removed in the next major version.
836 #[deprecated(since = "1.17.0", note = "use `gen_fn_signatures_with_mapper` instead")]
837 #[cfg(feature = "metadata")]
838 #[inline(always)]
839 pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
840 self.gen_fn_signatures_with_mapper(Into::into)
841 }
842}
843
844#[cfg(not(feature = "no_index"))]
845use crate::plugin::*;
846
847#[cfg(not(feature = "no_index"))]
848#[export_module]
849pub mod deprecated_array_functions {
850 use crate::packages::array_basic::array_functions::*;
851 use crate::packages::array_basic::index_of_start_inner;
852 use crate::{Array, INT};
853
854 /// Iterate through all the elements in the array, applying a function named by `mapper` to each
855 /// element in turn, and return the results as a new array.
856 ///
857 /// # Deprecated API
858 ///
859 /// This method is deprecated and will be removed from the next major version.
860 /// Use `array.map(Fn("fn_name"))` instead.
861 ///
862 /// # Function Parameters
863 ///
864 /// A function with the same name as the value of `mapper` must exist taking these parameters:
865 ///
866 /// * `element`: copy of array element
867 /// * `index` _(optional)_: current index in the array
868 ///
869 /// # Example
870 ///
871 /// ```rhai
872 /// fn square(x) { x * x }
873 ///
874 /// fn multiply(x, i) { x * i }
875 ///
876 /// let x = [1, 2, 3, 4, 5];
877 ///
878 /// let y = x.map("square");
879 ///
880 /// print(y); // prints "[1, 4, 9, 16, 25]"
881 ///
882 /// let y = x.map("multiply");
883 ///
884 /// print(y); // prints "[0, 2, 6, 12, 20]"
885 /// ```
886 #[rhai_fn(name = "map", return_raw)]
887 pub fn map_by_fn_name(
888 ctx: NativeCallContext,
889 array: &mut Array,
890 mapper: &str,
891 ) -> RhaiResultOf<Array> {
892 map(ctx, array, FnPtr::new(mapper)?)
893 }
894 /// Iterate through all the elements in the array, applying a function named by `filter` to each
895 /// element in turn, and return a copy of all elements (in order) that return `true` as a new array.
896 ///
897 /// # Deprecated API
898 ///
899 /// This method is deprecated and will be removed from the next major version.
900 /// Use `array.filter(Fn("fn_name"))` instead.
901 ///
902 /// # Function Parameters
903 ///
904 /// A function with the same name as the value of `filter` must exist taking these parameters:
905 ///
906 /// * `element`: copy of array element
907 /// * `index` _(optional)_: current index in the array
908 ///
909 /// # Example
910 ///
911 /// ```rhai
912 /// fn screen(x, i) { x * i >= 10 }
913 ///
914 /// let x = [1, 2, 3, 4, 5];
915 ///
916 /// let y = x.filter("is_odd");
917 ///
918 /// print(y); // prints "[1, 3, 5]"
919 ///
920 /// let y = x.filter("screen");
921 ///
922 /// print(y); // prints "[12, 20]"
923 /// ```
924 #[rhai_fn(name = "filter", return_raw)]
925 pub fn filter_by_fn_name(
926 ctx: NativeCallContext,
927 array: &mut Array,
928 filter_func: &str,
929 ) -> RhaiResultOf<Array> {
930 filter(ctx, array, FnPtr::new(filter_func)?)
931 }
932 /// Iterate through all the elements in the array, applying a function named by `filter` to each
933 /// element in turn, and return the index of the first element that returns `true`.
934 /// If no element returns `true`, `-1` is returned.
935 ///
936 /// # Deprecated API
937 ///
938 /// This method is deprecated and will be removed from the next major version.
939 /// Use `array.index_of(Fn("fn_name"))` instead.
940 ///
941 /// # Function Parameters
942 ///
943 /// A function with the same name as the value of `filter` must exist taking these parameters:
944 ///
945 /// * `element`: copy of array element
946 /// * `index` _(optional)_: current index in the array
947 ///
948 /// # Example
949 ///
950 /// ```rhai
951 /// fn is_special(x) { x > 3 }
952 ///
953 /// fn is_dumb(x) { x > 8 }
954 ///
955 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
956 ///
957 /// print(x.index_of("is_special")); // prints 3
958 ///
959 /// print(x.index_of("is_dumb")); // prints -1
960 /// ```
961 #[rhai_fn(name = "index_of", return_raw, pure)]
962 pub fn index_of_by_fn_name(
963 ctx: NativeCallContext,
964 array: &mut Array,
965 filter: &str,
966 ) -> RhaiResultOf<INT> {
967 if array.is_empty() {
968 return Ok(-1);
969 }
970 if let Ok(fp) = FnPtr::new(filter) {
971 match index_of_start_inner(&ctx, array, fp, 0) {
972 Ok(found) => return Ok(found),
973 Err(err) => match *err {
974 EvalAltResult::ErrorFunctionNotFound(ref sig, ..)
975 if sig.starts_with(filter) => {}
976 EvalAltResult::ErrorInFunctionCall(_, _, ref inner, _) => match **inner {
977 EvalAltResult::ErrorFunctionNotFound(ref sig, ..)
978 if sig.starts_with(filter) => {}
979 _ => return Err(err),
980 },
981 _ => return Err(err),
982 },
983 }
984 }
985 index_of_starting_from(ctx, array, filter.into(), 0)
986 }
987 /// Iterate through all the elements in the array, starting from a particular `start` position,
988 /// applying a function named by `filter` to each element in turn, and return the index of the
989 /// first element that returns `true`. If no element returns `true`, `-1` is returned.
990 ///
991 /// * If `start` < 0, position counts from the end of the array (`-1` is the last element).
992 /// * If `start` < -length of array, position counts from the beginning of the array.
993 /// * If `start` ≥ length of array, `-1` is returned.
994 ///
995 /// # Deprecated API
996 ///
997 /// This method is deprecated and will be removed from the next major version.
998 /// Use `array.index_of(Fn("fn_name"), start)` instead.
999 ///
1000 /// # Function Parameters
1001 ///
1002 /// A function with the same name as the value of `filter` must exist taking these parameters:
1003 ///
1004 /// * `element`: copy of array element
1005 /// * `index` _(optional)_: current index in the array
1006 ///
1007 /// # Example
1008 ///
1009 /// ```rhai
1010 /// fn plural(x) { x > 1 }
1011 ///
1012 /// fn singular(x) { x < 2 }
1013 ///
1014 /// fn screen(x, i) { x * i > 20 }
1015 ///
1016 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1017 ///
1018 /// print(x.index_of("plural", 3)); // prints 5: 2 > 1
1019 ///
1020 /// print(x.index_of("singular", 9)); // prints -1: nothing < 2 past index 9
1021 ///
1022 /// print(x.index_of("plural", 15)); // prints -1: nothing found past end of array
1023 ///
1024 /// print(x.index_of("plural", -5)); // prints 9: -5 = start from index 8
1025 ///
1026 /// print(x.index_of("plural", -99)); // prints 1: -99 = start from beginning
1027 ///
1028 /// print(x.index_of("screen", 8)); // prints 10: 3 * 10 > 20
1029 /// ```
1030 #[rhai_fn(name = "index_of", return_raw, pure)]
1031 pub fn index_of_by_fn_name_starting_from(
1032 ctx: NativeCallContext,
1033 array: &mut Array,
1034 filter: &str,
1035 start: INT,
1036 ) -> RhaiResultOf<INT> {
1037 if array.is_empty() {
1038 return Ok(-1);
1039 }
1040
1041 if let Ok(fp) = FnPtr::new(filter) {
1042 match index_of_start_inner(&ctx, array, fp, start) {
1043 Ok(found) => return Ok(found),
1044 Err(err) => match *err {
1045 EvalAltResult::ErrorFunctionNotFound(ref sig, ..)
1046 if sig.starts_with(filter) => {}
1047 EvalAltResult::ErrorInFunctionCall(_, _, ref inner, _) => match **inner {
1048 EvalAltResult::ErrorFunctionNotFound(ref sig, ..)
1049 if sig.starts_with(filter) => {}
1050 _ => return Err(err),
1051 },
1052 _ => return Err(err),
1053 },
1054 }
1055 }
1056 index_of_starting_from(ctx, array, filter.into(), start)
1057 }
1058 /// Return `true` if any element in the array that returns `true` when applied a function named
1059 /// by `filter`.
1060 ///
1061 /// # Deprecated API
1062 ///
1063 /// This method is deprecated and will be removed from the next major version.
1064 /// Use `array.some(Fn("fn_name"))` instead.
1065 ///
1066 /// # Function Parameters
1067 ///
1068 /// A function with the same name as the value of `filter` must exist taking these parameters:
1069 ///
1070 /// * `element`: copy of array element
1071 /// * `index` _(optional)_: current index in the array
1072 ///
1073 /// # Example
1074 ///
1075 /// ```rhai
1076 /// fn large(x) { x > 3 }
1077 ///
1078 /// fn huge(x) { x > 10 }
1079 ///
1080 /// fn screen(x, i) { i > x }
1081 ///
1082 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1083 ///
1084 /// print(x.some("large")); // prints true
1085 ///
1086 /// print(x.some("huge")); // prints false
1087 ///
1088 /// print(x.some("screen")); // prints true
1089 /// ```
1090 #[rhai_fn(name = "some", return_raw, pure)]
1091 pub fn some_by_fn_name(
1092 ctx: NativeCallContext,
1093 array: &mut Array,
1094 filter: &str,
1095 ) -> RhaiResultOf<bool> {
1096 some(ctx, array, FnPtr::new(filter)?)
1097 }
1098 /// Return `true` if all elements in the array return `true` when applied a function named by `filter`.
1099 ///
1100 /// # Deprecated API
1101 ///
1102 /// This method is deprecated and will be removed from the next major version.
1103 /// Use `array.all(Fn("fn_name"))` instead.
1104 ///
1105 /// # Function Parameters
1106 ///
1107 /// A function with the same name as the value of `filter` must exist taking these parameters:
1108 ///
1109 /// * `element`: copy of array element
1110 /// * `index` _(optional)_: current index in the array
1111 ///
1112 /// # Example
1113 ///
1114 /// ```rhai
1115 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1116 ///
1117 /// print(x.all(|v| v > 3)); // prints false
1118 ///
1119 /// print(x.all(|v| v > 1)); // prints true
1120 ///
1121 /// print(x.all(|v, i| i > v)); // prints false
1122 /// ```
1123 #[rhai_fn(name = "all", return_raw, pure)]
1124 pub fn all_by_fn_name(
1125 ctx: NativeCallContext,
1126 array: &mut Array,
1127 filter: &str,
1128 ) -> RhaiResultOf<bool> {
1129 all(ctx, array, FnPtr::new(filter)?)
1130 }
1131 /// Remove duplicated _consecutive_ elements from the array that return `true` when applied a
1132 /// function named by `comparer`.
1133 ///
1134 /// # Deprecated API
1135 ///
1136 /// This method is deprecated and will be removed from the next major version.
1137 /// Use `array.dedup(Fn("fn_name"))` instead.
1138 ///
1139 /// No element is removed if the correct `comparer` function does not exist.
1140 ///
1141 /// # Function Parameters
1142 ///
1143 /// * `element1`: copy of the current array element to compare
1144 /// * `element2`: copy of the next array element to compare
1145 ///
1146 /// ## Return Value
1147 ///
1148 /// `true` if `element1 == element2`, otherwise `false`.
1149 ///
1150 /// # Example
1151 ///
1152 /// ```rhai
1153 /// fn declining(a, b) { a >= b }
1154 ///
1155 /// let x = [1, 2, 2, 2, 3, 1, 2, 3, 4, 3, 3, 2, 1];
1156 ///
1157 /// x.dedup("declining");
1158 ///
1159 /// print(x); // prints "[1, 2, 3, 4]"
1160 /// ```
1161 #[rhai_fn(name = "dedup", return_raw)]
1162 pub fn dedup_by_fn_name(
1163 ctx: NativeCallContext,
1164 array: &mut Array,
1165 comparer: &str,
1166 ) -> RhaiResultOf<()> {
1167 dedup_by_comparer(ctx, array, FnPtr::new(comparer)?);
1168 Ok(())
1169 }
1170 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1171 ///
1172 /// # Deprecated API
1173 ///
1174 /// This method is deprecated and will be removed from the next major version.
1175 /// Use `array.reduce(Fn("fn_name"))` instead.
1176 ///
1177 /// # Function Parameters
1178 ///
1179 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1180 ///
1181 /// * `result`: accumulated result, initially `()`
1182 /// * `element`: copy of array element
1183 /// * `index` _(optional)_: current index in the array
1184 ///
1185 /// # Example
1186 ///
1187 /// ```rhai
1188 /// fn process(r, x) {
1189 /// x + (r ?? 0)
1190 /// }
1191 /// fn process_extra(r, x, i) {
1192 /// x + i + (r ?? 0)
1193 /// }
1194 ///
1195 /// let x = [1, 2, 3, 4, 5];
1196 ///
1197 /// let y = x.reduce("process");
1198 ///
1199 /// print(y); // prints 15
1200 ///
1201 /// let y = x.reduce("process_extra");
1202 ///
1203 /// print(y); // prints 25
1204 /// ```
1205 #[rhai_fn(name = "reduce", return_raw, pure)]
1206 pub fn reduce_by_fn_name(
1207 ctx: NativeCallContext,
1208 array: &mut Array,
1209 reducer: &str,
1210 ) -> RhaiResult {
1211 reduce(ctx, array, FnPtr::new(reducer)?)
1212 }
1213 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1214 ///
1215 /// # Deprecated API
1216 ///
1217 /// This method is deprecated and will be removed from the next major version.
1218 /// Use `array.reduce(Fn("fn_name"), initial)` instead.
1219 ///
1220 /// # Function Parameters
1221 ///
1222 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1223 ///
1224 /// * `result`: accumulated result, starting with the value of `initial`
1225 /// * `element`: copy of array element
1226 /// * `index` _(optional)_: current index in the array
1227 ///
1228 /// # Example
1229 ///
1230 /// ```rhai
1231 /// fn process(r, x) { x + r }
1232 ///
1233 /// fn process_extra(r, x, i) { x + i + r }
1234 ///
1235 /// let x = [1, 2, 3, 4, 5];
1236 ///
1237 /// let y = x.reduce("process", 5);
1238 ///
1239 /// print(y); // prints 20
1240 ///
1241 /// let y = x.reduce("process_extra", 5);
1242 ///
1243 /// print(y); // prints 30
1244 /// ```
1245 #[rhai_fn(name = "reduce", return_raw, pure)]
1246 pub fn reduce_by_fn_name_with_initial(
1247 ctx: NativeCallContext,
1248 array: &mut Array,
1249 reducer: &str,
1250 initial: Dynamic,
1251 ) -> RhaiResult {
1252 reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1253 }
1254 /// Reduce an array by iterating through all elements, in _reverse_ order,
1255 /// while applying a function named by `reducer`.
1256 ///
1257 /// # Deprecated API
1258 ///
1259 /// This method is deprecated and will be removed from the next major version.
1260 /// Use `array.reduce_rev(Fn("fn_name"))` instead.
1261 ///
1262 /// # Function Parameters
1263 ///
1264 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1265 ///
1266 /// * `result`: accumulated result, initially `()`
1267 /// * `element`: copy of array element
1268 /// * `index` _(optional)_: current index in the array
1269 ///
1270 /// # Example
1271 ///
1272 /// ```rhai
1273 /// fn process(r, x) {
1274 /// x + (r ?? 0)
1275 /// }
1276 /// fn process_extra(r, x, i) {
1277 /// x + i + (r ?? 0)
1278 /// }
1279 ///
1280 /// let x = [1, 2, 3, 4, 5];
1281 ///
1282 /// let y = x.reduce_rev("process");
1283 ///
1284 /// print(y); // prints 15
1285 ///
1286 /// let y = x.reduce_rev("process_extra");
1287 ///
1288 /// print(y); // prints 25
1289 /// ```
1290 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1291 pub fn reduce_rev_by_fn_name(
1292 ctx: NativeCallContext,
1293 array: &mut Array,
1294 reducer: &str,
1295 ) -> RhaiResult {
1296 reduce_rev(ctx, array, FnPtr::new(reducer)?)
1297 }
1298 /// Reduce an array by iterating through all elements, in _reverse_ order,
1299 /// while applying a function named by `reducer`.
1300 ///
1301 /// # Deprecated API
1302 ///
1303 /// This method is deprecated and will be removed from the next major version.
1304 /// Use `array.reduce_rev(Fn("fn_name"), initial)` instead.
1305 ///
1306 /// # Function Parameters
1307 ///
1308 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1309 ///
1310 /// * `result`: accumulated result, starting with the value of `initial`
1311 /// * `element`: copy of array element
1312 /// * `index` _(optional)_: current index in the array
1313 ///
1314 /// # Example
1315 ///
1316 /// ```rhai
1317 /// fn process(r, x) { x + r }
1318 ///
1319 /// fn process_extra(r, x, i) { x + i + r }
1320 ///
1321 /// let x = [1, 2, 3, 4, 5];
1322 ///
1323 /// let y = x.reduce_rev("process", 5);
1324 ///
1325 /// print(y); // prints 20
1326 ///
1327 /// let y = x.reduce_rev("process_extra", 5);
1328 ///
1329 /// print(y); // prints 30
1330 /// ```
1331 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1332 pub fn reduce_rev_by_fn_name_with_initial(
1333 ctx: NativeCallContext,
1334 array: &mut Array,
1335 reducer: &str,
1336 initial: Dynamic,
1337 ) -> RhaiResult {
1338 reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1339 }
1340 /// Sort the array based on applying a function named by `comparer`.
1341 ///
1342 /// # Deprecated API
1343 ///
1344 /// This method is deprecated and will be removed from the next major version.
1345 /// Use `array.sort(Fn("fn_name"))` instead.
1346 ///
1347 /// # Function Parameters
1348 ///
1349 /// A function with the same name as the value of `comparer` must exist taking these parameters:
1350 ///
1351 /// * `element1`: copy of the current array element to compare
1352 /// * `element2`: copy of the next array element to compare
1353 ///
1354 /// ## Return Value
1355 ///
1356 /// * Any integer > 0 if `element1 > element2`
1357 /// * Zero if `element1 == element2`
1358 /// * Any integer < 0 if `element1 < element2`
1359 ///
1360 /// # Example
1361 ///
1362 /// ```rhai
1363 /// fn reverse(a, b) {
1364 /// if a > b {
1365 /// -1
1366 /// } else if a < b {
1367 /// 1
1368 /// } else {
1369 /// 0
1370 /// }
1371 /// }
1372 /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1373 ///
1374 /// x.sort("reverse");
1375 ///
1376 /// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1377 /// ```
1378 #[rhai_fn(name = "sort", return_raw)]
1379 pub fn sort_by_fn_name(
1380 ctx: NativeCallContext,
1381 array: &mut Array,
1382 comparer: &str,
1383 ) -> RhaiResultOf<()> {
1384 sort_by(ctx, array, FnPtr::new(comparer)?)
1385 }
1386 /// Remove all elements in the array that returns `true` when applied a function named by `filter`
1387 /// and return them as a new array.
1388 ///
1389 /// # Deprecated API
1390 ///
1391 /// This method is deprecated and will be removed from the next major version.
1392 /// Use `array.drain(Fn("fn_name"))` instead.
1393 ///
1394 /// # Function Parameters
1395 ///
1396 /// A function with the same name as the value of `filter` must exist taking these parameters:
1397 ///
1398 /// * `element`: copy of array element
1399 /// * `index` _(optional)_: current index in the array
1400 ///
1401 /// # Example
1402 ///
1403 /// ```rhai
1404 /// fn small(x) { x < 3 }
1405 ///
1406 /// fn screen(x, i) { x + i > 5 }
1407 ///
1408 /// let x = [1, 2, 3, 4, 5];
1409 ///
1410 /// let y = x.drain("small");
1411 ///
1412 /// print(x); // prints "[3, 4, 5]"
1413 ///
1414 /// print(y); // prints "[1, 2]"
1415 ///
1416 /// let z = x.drain("screen");
1417 ///
1418 /// print(x); // prints "[3, 4]"
1419 ///
1420 /// print(z); // prints "[5]"
1421 /// ```
1422 #[rhai_fn(name = "drain", return_raw)]
1423 pub fn drain_by_fn_name(
1424 ctx: NativeCallContext,
1425 array: &mut Array,
1426 filter: &str,
1427 ) -> RhaiResultOf<Array> {
1428 drain(ctx, array, FnPtr::new(filter)?)
1429 }
1430 /// Remove all elements in the array that do not return `true` when applied a function named by
1431 /// `filter` and return them as a new array.
1432 ///
1433 /// # Deprecated API
1434 ///
1435 /// This method is deprecated and will be removed from the next major version.
1436 /// Use `array.retain(Fn("fn_name"))` instead.
1437 ///
1438 /// # Function Parameters
1439 ///
1440 /// A function with the same name as the value of `filter` must exist taking these parameters:
1441 ///
1442 /// * `element`: copy of array element
1443 /// * `index` _(optional)_: current index in the array
1444 ///
1445 /// # Example
1446 ///
1447 /// ```rhai
1448 /// fn large(x) { x >= 3 }
1449 ///
1450 /// fn screen(x, i) { x + i <= 5 }
1451 ///
1452 /// let x = [1, 2, 3, 4, 5];
1453 ///
1454 /// let y = x.retain("large");
1455 ///
1456 /// print(x); // prints "[3, 4, 5]"
1457 ///
1458 /// print(y); // prints "[1, 2]"
1459 ///
1460 /// let z = x.retain("screen");
1461 ///
1462 /// print(x); // prints "[3, 4]"
1463 ///
1464 /// print(z); // prints "[5]"
1465 /// ```
1466 #[rhai_fn(name = "retain", return_raw)]
1467 pub fn retain_by_fn_name(
1468 ctx: NativeCallContext,
1469 array: &mut Array,
1470 filter: &str,
1471 ) -> RhaiResultOf<Array> {
1472 retain(ctx, array, FnPtr::new(filter)?)
1473 }
1474}
1475
1476pub mod config {
1477 pub mod hashing {
1478 /// Set the hashing seed. This is used to hash functions etc.
1479 ///
1480 /// # Deprecated
1481 ///
1482 /// This method is deprecated.
1483 /// Use [`set_hashing_seed`][crate::config::hashing::set_hashing_seed] instead.
1484 ///
1485 /// This method will be removed in the next major version.
1486 #[deprecated(since = "1.17.0", note = "use `set_hashing_seed` instead")]
1487 #[inline(always)]
1488 pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>> {
1489 crate::config::hashing::set_hashing_seed(new_seed)
1490 }
1491
1492 /// Get the current hashing Seed.
1493 ///
1494 /// # Deprecated
1495 ///
1496 /// This method is deprecated.
1497 /// Use [`get_hashing_seed`][crate::config::hashing::get_hashing_seed] instead.
1498 ///
1499 /// This method will be removed in the next major version.
1500 #[deprecated(since = "1.17.0", note = "use `get_hashing_seed` instead")]
1501 #[inline]
1502 #[must_use]
1503 pub fn get_ahash_seed() -> &'static Option<[u64; 4]> {
1504 crate::config::hashing::get_hashing_seed()
1505 }
1506 }
1507}