Skip to content

Commit

Permalink
Changed some &str-parametres to impl AsRef<str>. (#652)
Browse files Browse the repository at this point in the history
Some API-functions are able to benefit from greater flexibility if `impl AsRef<str>` is expected rather than just `&str`.
  • Loading branch information
xacrimon authored and Lakelezz committed Jul 13, 2019
1 parent a8f0387 commit abd84c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,17 @@ impl Client {
/// # try_main().unwrap();
/// # }
/// ```
pub fn new<H>(token: &str, handler: H) -> Result<Self>
pub fn new<H>(token: impl AsRef<str>, handler: H) -> Result<Self>
where H: EventHandler + Send + Sync + 'static {

Self::new_with_handlers(token, Some(handler), None::<DummyRawEventHandler>)
Self::new_with_handlers(token.as_ref(), Some(handler), None::<DummyRawEventHandler>)
}
/// Creates a client with an optional Handler. If you pass `None`, events are never parsed, but
/// they can be received by registering a RawHandler.
pub fn new_with_handlers<H, RH>(token: &str, handler: Option<H>, raw_handler: Option<RH>) -> Result<Self>
pub fn new_with_handlers<H, RH>(token: impl AsRef<str>, handler: Option<H>, raw_handler: Option<RH>) -> Result<Self>
where H: EventHandler + Send + Sync + 'static,
RH: RawEventHandler + Send + Sync + 'static {
let token = token.trim();
let token = token.as_ref().trim();

let token = if token.starts_with("Bot ") {
token.to_string()
Expand Down Expand Up @@ -452,9 +452,9 @@ impl Client {
/// # }
/// ```
#[cfg(all(feature = "cache", feature = "http"))]
pub fn new_with_cache_update_timeout<H>(token: &str, handler: H, duration: Option<Duration>) -> Result<Self>
pub fn new_with_cache_update_timeout<H>(token: impl AsRef<str>, handler: H, duration: Option<Duration>) -> Result<Self>
where H: EventHandler + Send + Sync + 'static {
let token = token.trim();
let token = token.as_ref().trim();

let token = if token.starts_with("Bot ") {
token.to_string()
Expand Down Expand Up @@ -1047,7 +1047,9 @@ impl Client {
/// The type of failure is not specified.
///
/// [`ClientError::InvalidToken`]: enum.ClientError.html#variant.InvalidToken
pub fn validate_token(token: &str) -> Result<()> {
pub fn validate_token(token: impl AsRef<str>) -> Result<()> {
let token = token.as_ref();

if token.is_empty() {
return Err(Error::Client(ClientError::InvalidToken));
}
Expand Down
4 changes: 3 additions & 1 deletion src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ impl Message {
/// [`ModelError::MessageTooLong`]: ../error/enum.Error.html#variant.MessageTooLong
/// [Send Messages]: ../permissions/struct.Permissions.html#associatedconstant.SEND_MESSAGES
#[cfg(feature = "client")]
pub fn reply(&self, cache_http: impl CacheHttp, content: &str) -> Result<Message> {
pub fn reply(&self, cache_http: impl CacheHttp, content: impl AsRef<str>) -> Result<Message> {
let content = content.as_ref();

if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Model(ModelError::MessageTooLong(length_over)));
}
Expand Down
27 changes: 19 additions & 8 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ pub fn parse_invite(code: &str) -> &str {
/// ```
///
/// [`User`]: ../model/user/struct.User.html
pub fn parse_username(mention: &str) -> Option<u64> {
pub fn parse_username(mention: impl AsRef<str>) -> Option<u64> {
let mention = mention.as_ref();

if mention.len() < 4 {
return None;
}
Expand Down Expand Up @@ -174,7 +176,9 @@ pub fn parse_username(mention: &str) -> Option<u64> {
/// ```
///
/// [`Role`]: ../model/guild/struct.Role.html
pub fn parse_role(mention: &str) -> Option<u64> {
pub fn parse_role(mention: impl AsRef<str>) -> Option<u64> {
let mention = mention.as_ref();

if mention.len() < 4 {
return None;
}
Expand Down Expand Up @@ -211,7 +215,9 @@ pub fn parse_role(mention: &str) -> Option<u64> {
/// ```
///
/// [`Channel`]: ../model/channel/enum.Channel.html
pub fn parse_channel(mention: &str) -> Option<u64> {
pub fn parse_channel(mention: impl AsRef<str>) -> Option<u64> {
let mention = mention.as_ref();

if mention.len() < 4 {
return None;
}
Expand All @@ -237,7 +243,9 @@ pub fn parse_channel(mention: &str) -> Option<u64> {
/// assert_eq!(parse_mention("<@&137235212097683456>"), Some(137235212097683456));
/// assert_eq!(parse_mention("<#137234234728251392>"), Some(137234234728251392));
/// ```
pub fn parse_mention(mention: &str) -> Option<u64> {
pub fn parse_mention(mention: impl AsRef<str>) -> Option<u64> {
let mention = mention.as_ref();

if mention.starts_with("<@&") {
parse_role(mention)
} else if mention.starts_with("<@") || mention.starts_with("<@!") {
Expand Down Expand Up @@ -280,7 +288,9 @@ pub fn parse_mention(mention: &str) -> Option<u64> {
/// ```
///
/// [`Emoji`]: ../model/guild/struct.Emoji.html
pub fn parse_emoji(mention: &str) -> Option<EmojiIdentifier> {
pub fn parse_emoji(mention: impl AsRef<str>) -> Option<EmojiIdentifier> {
let mention = mention.as_ref();

let len = mention.len();

if len < 6 || len > 56 {
Expand Down Expand Up @@ -385,7 +395,8 @@ fn _read_image(path: &Path) -> Result<String> {
///
/// assert_eq!(parse_quotes(command), expected);
/// ```
pub fn parse_quotes(s: &str) -> Vec<String> {
pub fn parse_quotes(s: impl AsRef<str>) -> Vec<String> {
let s = s.as_ref();
let mut args = vec![];
let mut in_string = false;
let mut escaping = false;
Expand Down Expand Up @@ -784,8 +795,8 @@ fn clean_users(cache: &RwLock<Cache>, s: &mut String, show_discriminator: bool,
/// [`ContentSafeOptions`]: struct.ContentSafeOptions.html
/// [`Cache`]: ../cache/struct.Cache.html
#[cfg(feature = "cache")]
pub fn content_safe(cache: impl AsRef<CacheRwLock>, s: &str, options: &ContentSafeOptions) -> String {
let mut s = s.to_string();
pub fn content_safe(cache: impl AsRef<CacheRwLock>, s: impl AsRef<str>, options: &ContentSafeOptions) -> String {
let mut s = s.as_ref().to_string();
let cache = cache.as_ref();

if options.clean_role {
Expand Down

0 comments on commit abd84c2

Please sign in to comment.