ferrule documentation[styled mode]
specrfcshome

capsules

Status: rfc


capsules

this feature is an rfc. the spec describes what it might be, not what's committed.


Overview

Capsule types represent unique resources that:


Non-Copy Semantics

Capsules cannot be implicitly copied:

const file: File = fs.open(path);
const copy = file;  // ERROR: File is a capsule type, cannot copy

If the type author provides a duplicator, explicit cloning is possible:

const copy = file.duplicate();  // only if File defines duplicate()

Finalization

On region disposal, capsules receive a finalize call:

type FileHandle = capsule {
  fd: i32,
  
  finalize: function(self) -> Unit {
    syscall.close(self.fd);
  }
};

Registration

Capsules are automatically registered with their owning region:

const heap = region.heap();
const handle: FileHandle = heap.create_capsule(FileHandle { fd: fd });
defer heap.dispose();  // handle.finalize() called here

Use Cases


Secure Zeroing

For sensitive data, use secure zeroing that is not optimized away:

mem.secure_zero(secret_view);

Capsules holding secrets should call this in their finalizer.


Constant-Time Operations

Types may be annotated as constant-time; branches on their values are linted:

type SecretKey = capsule {
  bytes: View<u8>,
  constant_time: true
};

// compiler warns if code branches on SecretKey contents