This commit is contained in:
zekroTJA committed 2026-07-15 22:20:36 +02:00
commit 0dc3a6c0f6
11 files changed
+243

No files matched your search

+1
View File
@@ -0,0 +1 @@
/target
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "problem"
version = "0.1.0"
edition = "2024"
[dependencies]
+39
View File
@@ -0,0 +1,39 @@
#![allow(unused)]
use std::collections::HashMap;
struct CredentialManager {
entries: HashMap<String, String>,
}
impl CredentialManager {
pub fn new() -> CredentialManager {
CredentialManager {
entries: HashMap::new(),
}
}
pub fn unlock(&self, master_secret: &str) {
todo!()
}
pub fn set(&mut self, key: &str, secret: &str) {
todo!()
}
pub fn get(&mut self, key: &str) -> Option<&str> {
todo!()
}
pub fn lock(&self) {
todo!()
}
}
fn main() {
let mut pw = CredentialManager::new();
pw.unlock("pssst");
pw.set("foo", "bar");
let val = pw.get("foo");
pw.lock();
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "runtime-check"
version = "0.1.0"
edition = "2024"
[dependencies]
+55
View File
@@ -0,0 +1,55 @@
#![allow(unused)]
use std::collections::HashMap;
struct CredentialManager {
locked: bool,
entries: HashMap<String, String>,
}
impl CredentialManager {
pub fn new() -> CredentialManager {
CredentialManager {
locked: false,
entries: HashMap::new(),
}
}
pub fn unlock(&self, master_secret: &str) -> Result<(), &'static str> {
if !self.locked {
return Err("already unlocked");
}
todo!()
}
pub fn set(&mut self, key: &str, secret: &str) -> Result<(), &'static str> {
if self.locked {
return Err("locked");
}
todo!()
}
pub fn get(&mut self, key: &str) -> Result<Option<&str>, &'static str> {
if self.locked {
return Err("locked");
}
todo!()
}
pub fn lock(&self) -> Result<(), &'static str> {
if self.locked {
return Err("already locked");
}
todo!()
}
}
fn main() -> Result<(), &'static str> {
let mut pw = CredentialManager::new();
pw.unlock("pssst")?;
pw.set("foo", "bar")?;
let val = pw.get("foo")?;
pw.lock()?;
Ok(())
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "builder-pattern"
version = "0.1.0"
edition = "2024"
[dependencies]
+49
View File
@@ -0,0 +1,49 @@
#![allow(unused)]
use std::collections::HashMap;
struct LockedCredentialManager {
entries: HashMap<String, String>,
}
struct UnlockedCredentialManager {
entries: HashMap<String, String>,
}
impl LockedCredentialManager {
pub fn new() -> LockedCredentialManager {
LockedCredentialManager {
entries: HashMap::new(),
}
}
pub fn unlock(self, master_secret: &str) -> UnlockedCredentialManager {
UnlockedCredentialManager {
entries: self.entries,
}
}
}
impl UnlockedCredentialManager {
pub fn set(&mut self, key: &str, secret: &str) {
todo!()
}
pub fn get(&mut self, key: &str) -> Option<&str> {
todo!()
}
pub fn lock(self) -> LockedCredentialManager {
LockedCredentialManager {
entries: self.entries,
}
}
}
fn main() {
let pw = LockedCredentialManager::new();
let mut pw = pw.unlock("pssst");
pw.set("foo", "bar");
let val = pw.get("foo");
pw.lock();
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "typestate"
version = "0.1.0"
edition = "2024"
[dependencies]
+54
View File
@@ -0,0 +1,54 @@
#![allow(unused)]
use std::collections::HashMap;
struct Locked;
struct Unlocked;
struct CredentialManager<State = Locked> {
entries: HashMap<String, String>,
state: std::marker::PhantomData<State>,
}
impl CredentialManager {
pub fn new() -> CredentialManager<Locked> {
CredentialManager {
entries: HashMap::new(),
state: std::marker::PhantomData,
}
}
}
impl CredentialManager<Locked> {
pub fn unlock(self, master_secret: &str) -> CredentialManager<Unlocked> {
CredentialManager {
entries: self.entries,
state: std::marker::PhantomData,
}
}
}
impl CredentialManager<Unlocked> {
pub fn set(&mut self, key: &str, secret: &str) {
todo!()
}
pub fn get(&mut self, key: &str) -> Option<&str> {
todo!()
}
pub fn lock(self) -> CredentialManager<Locked> {
CredentialManager {
entries: self.entries,
state: std::marker::PhantomData,
}
}
}
fn main() {
let pw = CredentialManager::new();
let mut pw = pw.unlock("pssst");
pw.set("foo", "bar");
let val = pw.get("foo");
pw.lock();
}
Generated
+19
View File
@@ -0,0 +1,19 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "builder-pattern"
version = "0.1.0"
[[package]]
name = "problem"
version = "0.1.0"
[[package]]
name = "runtime-check"
version = "0.1.0"
[[package]]
name = "typestate"
version = "0.1.0"
+2
View File
@@ -0,0 +1,2 @@
[workspace]
members = ["01-problem", "02-runtime-check", "03-builder-pattern", "04-typestate"]