menu

manual-struct-conversion

Prefer conversions over field-by-field reconstruction

Reconstructing every field of one nominal value from an equally shaped value repeats a conversion wherever it is needed. Instead, you SHOULD implement From for the destination type and call its from method.

Reported

1struct Input {2    x: int32;3    y: int32;4}5struct Point {6    x: int32;7    y: int32;8}9 10function convert(input: Input): Point {11    return Point { x: input.x, y: input.y };12}

Accepted

1struct Input {2    x: int32;3    y: int32;4}5struct Point {6    x: int32;7    y: int32;8}9 10extension of Point implements From<Input> {11    static from(input: Input): Point {12        return Point { x: input.x, y: input.y };13    }14}15 16function convert(input: Input): Point {17    return Point.from(input);18}