Skip to content

Instantly share code, notes, and snippets.

@Vild
Created August 6, 2018 17:16
Show Gist options
  • Select an option

  • Save Vild/27f16320e1dd9ab830a9f8946ad8f0e4 to your computer and use it in GitHub Desktop.

Select an option

Save Vild/27f16320e1dd9ab830a9f8946ad8f0e4 to your computer and use it in GitHub Desktop.
Struct extend example
/**
* The filesystem base types
*
* Copyright: © 2015-2017, Dan Printzell
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, Mozilla Public License Version 2.0)
* (See accompanying file LICENSE)
* Authors: $(LINK2 https://vild.io/, Dan Printzell)
*/
module fs.block;
import fs;
import stl.address;
import stl.io.log;
import stl.vmm.heap;
/**
* The block representation struct.
* It contains a byte array that define how big each block is.
*/
@safe struct FSBlock {
/**
* The size of each block.
*/
enum blockSize = 0x1000;
/// The data
ubyte[blockSize] data;
}
/**
* Helper class for writing and reading blocks from the file.
*/
@safe struct FSBlockDevice {
/**
* The block index type.
*/
alias BlockID = ulong;
/**
* This functions reads a block at the index \a idx from the blockdevice and writes the data to \a block.
* Params:
* idx The blocks index
* block Where to write the block to
* See_Also:
* FSBlock
*/
void delegate(FSBlockDevice.BlockID idx, ref FSBlock block) readBlock;
/**
* This functions writes the block \a block to the index \a idx in the blockdevice.
* Params:
* idx The blocks index
* block The block
* See_Also:
* FSBlock
*/
void delegate(FSBlockDevice.BlockID idx, const ref FSBlock block) writeBlock;
/**
* Get the number of block the device have.
*/
size_t delegate() getBlockCount;
}
/**
* The filesystem base
*
* Copyright: © 2015-2017, Dan Printzell
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, Mozilla Public License Version 2.0)
* (See accompanying file LICENSE)
* Authors: $(LINK2 https://vild.io/, Dan Printzell)
*/
module fs.tarfs.block;
import fs.tarfs;
import stl.address;
import stl.io.log;
import stl.vmm.heap;
@safe struct TarFSBlockDevice {
import stl.address : VirtMemoryRange;
FSBlockDevice base;
alias base this;
VirtMemoryRange data;
this(VirtMemoryRange range) {
data = range;
with (base) {
readBlock = &this.readBlock;
writeBlock = &this.writeBlock;
getBlockCount = &this.getBlockCount;
}
}
void readBlock(FSBlockDevice.BlockID idx, ref FSBlock block) {
Log.error("TarFSBlockDevice.readBlock: ", idx);
block = FSBlock();
}
void writeBlock(FSBlockDevice.BlockID idx, const ref FSBlock block) {
Log.error("TarFSBlockDevice.writeBlock: ", idx);
}
size_t getBlockCount() {
Log.error("TarFSBlockDevice.getBlockCount: ");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment