Skip to content

Instantly share code, notes, and snippets.

@tjdevries
Last active February 23, 2023 18:42
Show Gist options
  • Save tjdevries/974da92fd3a197f25c5e9c9551d8fdf0 to your computer and use it in GitHub Desktop.
Save tjdevries/974da92fd3a197f25c5e9c9551d8fdf0 to your computer and use it in GitHub Desktop.
/// Proposed
/// A file which may or may not be async
struct ?async File {
file_descriptor: std::os::RawFd, // shared field in all contexts
async waker: Waker, // field only available in async contexts
!async meta: Metadata, // field only available in non-async contexts
}
impl ?async File {
/// Attempts to open a file in read-only mode.
?async fn open(path: Path) -> io::Result<Self> {
if is_async() { // compiler built-in function
// create an async `File` here; can use `.await`
} else {
// create a non-async `File` here
}
}
}
/// My Bad Idea?
struct async(Either) File {
file_descriptor: std::os::RawFd, // shared field in all contexts
// or maybe async(Enabled)
async(Required) waker: Waker, // field only available in async contexts
// or maybe async(Disabled)
async(Never) Metadata, // field only available in non-async contexts
}
impl async(Either) File {
/// Attempts to open a file in read-only mode.
async(Either) fn open(path: Path) -> io::Result<Self> {
match async_context() {
async::Enabled => { /* create an async `File` here; can use `.await` */ },
async::Disabled => { /* create a non-async `File` here */ },
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment