Skip to content

Commit

Permalink
Fix psuedo code [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Mar 30, 2024
1 parent 7d422ad commit bee3c73
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions book/src/10_struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ S3 methods such as `print.<your struct>()` if necessary.
```r
class(p)
#> [1] "Person"

# register print() S3 method for Person
print.Person <- function(x, ...) print(x$name())
registerS3method("print", "Person", print.Person)

p
#> たかし
```

### Struct output
Expand Down Expand Up @@ -203,20 +210,26 @@ copying. For example, consider there's a type `HeavyData`, which contains huge
size of data, and `HeavyDataBundle` which bundles two `HeavyData`s.

```rust
#[derive(Clone)]
struct HeavyData(Vec<i32>);

struct HeavyDataBundle {
data1: HeavyData,
data2: HeavyData,
};
}

#[savvy]
impl HeavyData {
// ...snip...
}
```

`HeavyDataBundle` requires the ownership of the `DataBundle`s. So, if the input
is `&`, you need to `clone()` the data, which can be costly.

```rust
#[savvy]
impl HeavyDataBundle(Vec<i32>) {
impl HeavyDataBundle {
fn new(
data1: &HeavyData,
data2: &HeavyData,
Expand All @@ -233,7 +246,7 @@ In this case, you can move the ownership to avoid copying.

```rust
#[savvy]
impl HeavyDataBundle(Vec<i32>) {
impl HeavyDataBundle {
fn new(
data1: HeavyData,
data2: HeavyData,
Expand Down

0 comments on commit bee3c73

Please sign in to comment.