Skip to content

Commit

Permalink
[Examples] Add examples of how to map over generic and labelled repre…
Browse files Browse the repository at this point in the history
…sentations (#192)

* [Examples] Add examples of how to map over generic and labelled representations

```
❯ cargo run --example generic
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/examples/generic`
Joe
First name: bo
Last name: peep
age: 30
❯ cargo run --example labelled
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/examples/labelled`
Blow
ExternalPerson {
    age: 10,
    address: ExternalAddress {
        name: "somewhere out there",
        phone: ExternalPhoneNumber {
            main: 1234,
        },
    },
    name: "John",
}
first_name: bo
last_name: peep
age: 30
```
  • Loading branch information
lloydmeta committed Jul 17, 2021
1 parent 2b16d2c commit 8d82a7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,11 @@ jobs:
with:
command: build
- uses: actions-rs/cargo@v1
name: std build generic example
name: std build examples
if: ${{ matrix.use_std == 'n' }}
with:
command: build
args: --example generic
- uses: actions-rs/cargo@v1
name: std build labelled example
if: ${{ matrix.use_std == 'n' }}
with:
command: build
args: --example labelled
args: --examples

test:
name: Test Suite
Expand Down
14 changes: 14 additions & 0 deletions examples/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ fn main() {
..p
});
assert_eq!(oldest_person.age, 90);

// mapping over generic representation
let peep = Person {
first_name: "bo",
last_name: "peep",
age: 30,
};
let generic = frunk::into_generic(peep);
// mapping each one
let _ = generic.map(hlist![
|first_name| println!("First name: {}", first_name),
|last_name| println!("Last name: {}", last_name),
|age| println!("age: {}", age),
]);
}
14 changes: 14 additions & 0 deletions examples/labelled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ fn main() {

let external_person: ExternalPerson = internal_person.transmogrify();
println!("{:#?}", external_person);

// mapping over labelled generic representation
let peep = NewUser {
first_name: "bo",
last_name: "peep",
age: 30,
};
let labelled_generic = frunk::into_labelled_generic(peep);

let _ = labelled_generic.map(hlist![
|f: Field<_, _>| println!("{}: {}", f.name, f.value),
|l: Field<_, _>| println!("{}: {}", l.name, l.value),
|a: Field<_, _>| println!("{}: {}", a.name, a.value),
]);
}

0 comments on commit 8d82a7b

Please sign in to comment.