Crates.io | dynamic-list |
lib.rs | dynamic-list |
version | 0.3.0 |
source | src |
created_at | 2023-11-27 20:24:15.355283 |
updated_at | 2023-12-07 19:14:54.184913 |
description | A powerful and efficient implementation of dynamic lists with versatile data structures, capable of storing any type! |
homepage | https://github.com/ferranSanchezLlado/dynamic-list.git |
repository | https://github.com/ferranSanchezLlado/dynamic-list.git |
max_upload_size | |
id | 1050905 |
size | 36,343 |
A powerful and efficient implementation of dynamic lists with versatile data structures. It is designed to store any type without incurring extra costs, making it an ideal choice for a wide range of applications. One of the main advantages is the you would avoid the extra cost of dynamic dispatch.
Currently, two implementations are provided:
Add the following to your Cargo.toml
:
[dependencies]
dynamic-list = "0.3.0"
Or if you want to use the latest version from the master branch:
[dependencies]
dynamic-list = { git = "https://github.com/ferranSanchezLlado/dynamic-list.git" }
The main way to interact with the DynamicList
is through the implementation of a trait that allows it to iterate, reduce, or accumulate by recursively calling itself.
Example 1: Get a specific element
use dynamic_list::*;
use typenum::*;
// Chain access:
let array = array![1u8, "hello", true, "world"];
assert_eq!(array.forward().next().value(), &"hello");
assert_eq!(array.backward().value(), &"world");
// Index access:
let list = list![1u8, "hello", true, "world"];
assert_eq!(Index::<U1>::index(list.forward()), &"hello");
assert_eq!(Index::<U3>::index(list.forward()), &"world");
Example 2: We want to concatenate a list of items into a single string:
use dynamic_list::{list::Node, *};
// Iterator trait
trait Concat {
fn concat(&self) -> String;
}
impl<V: ToString + Clone> Concat for Node<V> {
fn concat(&self) -> String {
self.value().to_string()
}
}
impl<V: ToString + Clone, N: Concat> Concat for Node<V, N> {
fn concat(&self) -> String {
format!("{}{}", self.value().to_string(), self.next().concat())
}
}
let list = list![1u8, "_hello", -3i32];
assert_eq!(list.forward().concat(), "1_hello-3");
assert_eq!(list.backward().concat(), "-3_hello1");
Example 3: We want to count how many even numbers are on the list:
use dynamic_list::{list::Node, *};
// Polymorphic trait
trait Even {
fn even(&self) -> usize;
}
impl<T: Clone + TryInto<usize>> Even for T {
fn even(&self) -> usize {
(self.clone().try_into().unwrap_or(1) + 1) % 2
}
}
// Iterator trait
trait NumberEven {
fn evens(&self) -> usize;
}
impl<V: Even> NumberEven for Node<V> {
fn evens(&self) -> usize {
self.value().even()
}
}
impl<V: Even + Clone, N: NumberEven> NumberEven for Node<V, N> {
fn evens(&self) -> usize {
self.value().even() + self.next().evens()
}
}
let list = list![false, 1, 2u8, -3, 4isize];
assert_eq!(list.forward().evens(), 3);
While Dynamic List provides a powerful and flexible solution, it's essential to be aware of the following limitations:
This project is licensed under the MIT License or Apache License, Version 2.0 at your option.