Skip to content

Commit

Permalink
tuple get
Browse files Browse the repository at this point in the history
  • Loading branch information
2A5F committed Nov 21, 2023
1 parent 69a4aa5 commit c452b11
Show file tree
Hide file tree
Showing 6 changed files with 3,023 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

Provides many useful tools related to tuples

- Support no-std and wasm
- AsRef
- AsMut
- AsOption
- AsResult
- AsDeref
- AsDerefMut
- TupleGet
- Transpose Option
- Transpose Result
- Cloned
Expand Down Expand Up @@ -78,6 +80,14 @@ Provides many useful tools related to tuples
let b = ();
assert_eq!(b.arity(), 0);
```
- get
```rust
let a = (1, 2, 3, 4, 5);
assert_eq!(*a.get(2), 3);

let mut a = (1, 2, 3, 4, 5);
*a.get_mut(3) = 6;
```
- iter
```rust
let a = (1, 2, 3)
Expand Down
59 changes: 59 additions & 0 deletions code_gen/src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn code_gen(out_dir: &Path) {
gen_call(&ctx, &out_dir);
gen_apply_tuple(&ctx, &out_dir);
gen_capt(&ctx, &out_dir);
gen_tuple_get(&ctx, &out_dir);
}

#[allow(dead_code)]
Expand Down Expand Up @@ -1344,3 +1345,61 @@ fn gen_capt_size(ctx: &Ctx, size: usize) -> TokenStream {
};
tks
}

fn gen_tuple_get(ctx: &Ctx, out_dir: &Path) {
let items = (1..33usize).map(|i| gen_tuple_get_size(ctx, i));
let tks = quote! { #(#items)* };
let mut code = tks.to_string();
code.insert_str(0, "// This file is by code gen, do not modify\n\n");
let dest_path = Path::new(out_dir).join("tuple_get.rs");
fs::write(&dest_path, code).unwrap();
}

fn gen_tuple_get_size(ctx: &Ctx, size: usize) -> TokenStream {
let ts = &ctx.ts[0..size];
let size_lits = &ctx.size_lits[0..size];
let take: Vec<_> = size_lits.iter().map(|i| quote! { #i => &self.#i, }).collect();
let take_some: Vec<_> = size_lits.iter().map(|i| quote! { #i => Some(&self.#i), }).collect();
let take_mut: Vec<_> = size_lits.iter().map(|i| quote! { #i => &mut self.#i, }).collect();
let take_mut_some: Vec<_> = size_lits.iter().map(|i| quote! { #i => Some(&mut self.#i), }).collect();

let tks = quote! {
impl<T> TupleGet for (#(#ts,)*) {
type Output = T;

fn get(&self, index: usize) -> &Self::Output {
match index {
#(#take)*
_ => panic!("index out of bounds: the len is {} bug the index is {}", #size, index),
}
}


fn try_get(&self, index: usize) -> Option<&Self::Output> {
match index {
#(#take_some)*
_ => None,
}
}
}

impl<T> TupleGetMut for (#(#ts,)*) {

fn get_mut(&mut self, index: usize) -> &mut Self::Output {
match index {
#(#take_mut)*
_ => panic!("index out of bounds: the len is {} bug the index is {}", #size, index),
}
}


fn try_get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
match index {
#(#take_mut_some)*
_ => None,
}
}
}
};
tks
}
6 changes: 4 additions & 2 deletions tuples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ license = "MIT"
name = "tuples"
readme = "../README.md"
repository = "https://github.com/libsugar/tuplers"
version = "1.12.0"
version = "1.13.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[features]
apply_tuple = []
capt = []
cloned = []
combin = []
default = [
Expand All @@ -33,6 +34,7 @@ default = [
"tuple_call",
"re-exports",
"capt",
"tuple_get",
]
flatten = []
re-exports = []
Expand All @@ -46,7 +48,7 @@ split_to_tuple_by = []
transpose = []
tuple_as = []
tuple_call = []
tuple_get = []
tuple_iter = []
tuple_map = []
tuple_meta = []
capt = []
Loading

0 comments on commit c452b11

Please sign in to comment.