You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define DocumentId and DatabaseName types. These types would replace the &str type currently used for identifying documents and databases, thereby making our crate's API more type-safe.
The text was updated successfully, but these errors were encountered:
In the example below, our crate defines the DatabaseName trait and the DatabaseLiteral type, and the application defines the Db type to implement DatabaseName.
Doing it this way allows applications to specify a plain string literal (using the DatabaseLiteral type), while also allowing applications to implement custom database name types of unlimited complexity.
trait DatabaseName {
fn database_name(&self) -> &str;
}
struct DatabaseLiteral<'a>(&'a str);
impl<'a> DatabaseName for DatabaseLiteral<'a> {
fn database_name(&self) -> &str {
let DatabaseLiteral(s) = *self;
s
}
}
enum Db {
Alpha,
Bravo(String),
}
impl DatabaseName for Db {
fn database_name(&self) -> &str {
match *self {
Db::Alpha => "alpha",
Db::Bravo(ref s) => s,
}
}
}
fn head_database<D>(name: D) -> () where D: DatabaseName {
println!("{}", name.database_name());
}
head_database(Db::Alpha);
head_database(Db::Bravo("hello".to_string()));
head_database(DatabaseLiteral("foo"));
Define
DocumentId
andDatabaseName
types. These types would replace the&str
type currently used for identifying documents and databases, thereby making our crate's API more type-safe.The text was updated successfully, but these errors were encountered: