pub trait IndexingMutationExt {
// Required methods
fn get_index_mut(&mut self, index: &Value) -> Result<&mut Value, Error>;
fn get_indices_mut(
&mut self,
index: &Value,
) -> Result<Vec<&mut Value>, Error>;
fn set_index(&mut self, index: &Value, value: Value) -> Result<(), Error>;
fn insert_at(&mut self, index: &Value, value: Value) -> Result<(), Error>;
fn delete_index(&mut self, index: &Value) -> Result<Value, Error>;
}Expand description
A trait for indexing operations that can mutate the value
Required Methods§
Sourcefn get_index_mut(&mut self, index: &Value) -> Result<&mut Value, Error>
fn get_index_mut(&mut self, index: &Value) -> Result<&mut Value, Error>
Get a value from an index
Returns a mutable reference to the value, or an Error::Index if the index is not found
§Examples
use polyvalue::{Value};
use polyvalue::types::{Object};
use polyvalue::operations::{IndexingOperationExt, IndexingMutationExt};
let mut b = Object::try_from(vec![("a", 1), ("b", 2)]).unwrap();
let index = Value::from("b");
let result = b.get_index_mut(&index).unwrap();
*result = Value::from(3);
assert_eq!(b.get_index(&index).unwrap(), Value::from(3));Sourcefn get_indices_mut(&mut self, index: &Value) -> Result<Vec<&mut Value>, Error>
fn get_indices_mut(&mut self, index: &Value) -> Result<Vec<&mut Value>, Error>
Get values from one or more indices, mutably
Returns a vector of references to the values, or an Error::Index if any of the indices are not found
Acts as a convenience wrapper around get_index where array values are treated as a set of indices
Sourcefn set_index(&mut self, index: &Value, value: Value) -> Result<(), Error>
fn set_index(&mut self, index: &Value, value: Value) -> Result<(), Error>
Set a value at an index
Returns an Error::Index if the index is not found
§Examples
use polyvalue::{Value};
use polyvalue::operations::{IndexingOperationExt, IndexingMutationExt};
let mut a = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
let index = Value::from(1);
a.set_index(&index, Value::from(4)).unwrap();
assert_eq!(a.get_index(&index).unwrap(), Value::from(4));Sourcefn insert_at(&mut self, index: &Value, value: Value) -> Result<(), Error>
fn insert_at(&mut self, index: &Value, value: Value) -> Result<(), Error>
Insert a value at an index
Returns an Error::Index if the index is out of bounds
§Examples
use polyvalue::{Value};
use polyvalue::operations::{IndexingOperationExt, IndexingMutationExt};
let mut a = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
let index = Value::from(1);
a.insert_at(&index, Value::from(4)).unwrap();
assert_eq!(a.get_index(&index).unwrap(), Value::from(4));Sourcefn delete_index(&mut self, index: &Value) -> Result<Value, Error>
fn delete_index(&mut self, index: &Value) -> Result<Value, Error>
Delete a value at an index
Returns an Error::Index if the index is not found
§Examples
use polyvalue::{Value};
use polyvalue::operations::{IndexingOperationExt, IndexingMutationExt};
let mut a = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
let index = Value::from(1);
a.delete_index(&index).unwrap();