Skip to content

Commit

Permalink
#105 Iden trait thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 15, 2021
1 parent 21e99a9 commit 4e85c58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ jobs:
command: build
args: --all-features

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=thread-safe

- uses: actions-rs/cargo@v1
with:
command: build
Expand Down
41 changes: 25 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,36 @@ pub use std::sync::Arc as SeaRc;
#[cfg(not(feature = "thread-safe"))]
pub use std::rc::Rc as SeaRc;

/// Identifier in query
pub trait Iden {
fn prepare(&self, s: &mut dyn fmt::Write, q: char) {
write!(s, "{}{}{}", q, self.quoted(q), q).unwrap();
}
macro_rules! iden_trait {
($($bounds:ident),*) => {
/// Identifier
pub trait Iden where $(Self: $bounds),* {
fn prepare(&self, s: &mut dyn fmt::Write, q: char) {
write!(s, "{}{}{}", q, self.quoted(q), q).unwrap();
}

fn quoted(&self, q: char) -> String {
let mut b = [0; 4];
let qq: &str = q.encode_utf8(&mut b);
self.to_string().replace(qq, qq.repeat(2).as_str())
}
fn quoted(&self, q: char) -> String {
let mut b = [0; 4];
let qq: &str = q.encode_utf8(&mut b);
self.to_string().replace(qq, qq.repeat(2).as_str())
}

fn to_string(&self) -> String {
let s = &mut String::new();
self.unquoted(s);
s.to_owned()
}
fn to_string(&self) -> String {
let s = &mut String::new();
self.unquoted(s);
s.to_owned()
}

fn unquoted(&self, s: &mut dyn fmt::Write);
fn unquoted(&self, s: &mut dyn fmt::Write);
}
};
}

#[cfg(feature = "thread-safe")]
iden_trait!(Send, Sync);
#[cfg(not(feature = "thread-safe"))]
iden_trait!();

pub type DynIden = SeaRc<dyn Iden>;

pub trait IntoIden {
Expand Down

0 comments on commit 4e85c58

Please sign in to comment.