I've been racking my brain with this for some time, and have been unable to increment and decrement specific fields inside nested structs as per the example below.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract CalendarGasTest {
struct ExampleNested_1 {
uint88 e0;
uint80 e1;
uint80 e2;
}
struct ExampleNested_2 {
uint80 e3;
uint72 e4;
uint72 e5;
}
struct Example {
ExampleNested_1 nested1;
ExampleNested_2 nested2;
}
Example public _example;
constructor() {}
function increment() external {
assembly {
let slot := _example.slot
// how do i increment a specific field in my packed structs?
}
}
function decrement() external {
assembly {
let slot := _example.slot
// how do i decrement a specific field in my packed structs?
}
}
}
I am able to get the first field of each nested struct to increment/decrement, with something like this:
function testFirstFieldFirstNestedStruct() external {
assembly {
let slot := _example.slot
sstore(slot, add(sload(slot), 1))
}
}
function testFirstFieldSecondNestedStruct() external {
assembly {
let slot := _example.slot
sstore(add(slot, 0x1), add(sload(add(slot, 0x1)), 1))
}
}
which I'm pretty sure is not a good implementation as I was just messing around (not sure why 0x1, 0x2, 0x3...0xn correspond to the nth nested struct).
Regardless of what I do I can't get the behavior I want which is to be able to increment and decrement any field within the nested structs.