Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model/id)!: add const id creation api #1402

Merged
merged 15 commits into from
Dec 31, 2021
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.53"
msrv = "1.57"
2 changes: 1 addition & 1 deletion .github/workflows/check-lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
name: Minimum supported Rust version
runs-on: ubuntu-latest
env:
minrust: 1.53
minrust: 1.57

steps:
- name: Checkout sources
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ functionality. Please use the individual crates listed below instead!

## Installation

Twilight supports a MSRV of Rust 1.53+.
Twilight supports a MSRV of Rust 1.57+.

We recommend that most users start out with these crates:

Expand Down Expand Up @@ -230,7 +230,7 @@ All first-party crates are licensed under [ISC][LICENSE.md]
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[logo]: https://raw.githubusercontent.com/twilight-rs/twilight/main/logo.png
[rust badge]: https://img.shields.io/badge/rust-1.53+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.57+-93450a.svg?style=for-the-badge&logo=rust
[`tracing-log`]: https://github.com/tokio-rs/tracing/tree/master/tracing-log
[`twilight-cache-inmemory`]: https://twilight.rs/chapter_1_crates/section_4_cache_inmemory.html
[`twilight-embed-builder`]: https://twilight.rs/chapter_1_crates/section_7_first_party/section_1_embed_builder.html
Expand Down
2 changes: 1 addition & 1 deletion cache/in-memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ All first-party crates are licensed under [ISC][LICENSE.md]
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.53+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.57+-93450a.svg?style=for-the-badge&logo=rust

<!-- cargo-sync-readme end -->
28 changes: 12 additions & 16 deletions cache/in-memory/src/event/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,23 @@ mod tests {
fn user_mod(id: Id<EmojiMarker>) -> Option<User> {
if id.get() % 2 == 0 {
// Only use user for half
Some(test::user(Id::new(1).expect("non zero")))
Some(test::user(Id::new(1)))
} else {
None
}
}

// Single inserts
{
let guild_1_emoji_ids = (1..=10)
.map(|n| Id::new(n).expect("non zero"))
.collect::<Vec<_>>();
let guild_1_emoji_ids = (1..=10).map(Id::new).collect::<Vec<_>>();
let guild_1_emoji = guild_1_emoji_ids
.iter()
.copied()
.map(|id| test::emoji(id, user_mod(id)))
.collect::<Vec<_>>();

for emoji in guild_1_emoji {
cache.cache_emoji(Id::new(1).expect("non zero"), emoji);
cache.cache_emoji(Id::new(1), emoji);
}

for id in guild_1_emoji_ids.iter().cloned() {
Expand All @@ -124,7 +122,7 @@ mod tests {

// Ensure the emoji has been added to the per-guild lookup map to prevent
// issues like #551 from returning
let guild_emojis = cache.guild_emojis(Id::new(1).expect("non zero"));
let guild_emojis = cache.guild_emojis(Id::new(1));
assert!(guild_emojis.is_some());
let guild_emojis = guild_emojis.unwrap();

Expand All @@ -134,22 +132,20 @@ mod tests {

// Bulk inserts
{
let guild_2_emoji_ids = (11..=20)
.map(|n| Id::new(n).expect("non zero"))
.collect::<Vec<_>>();
let guild_2_emoji_ids = (11..=20).map(Id::new).collect::<Vec<_>>();
let guild_2_emojis = guild_2_emoji_ids
.iter()
.copied()
.map(|id| test::emoji(id, user_mod(id)))
.collect::<Vec<_>>();
cache.cache_emojis(Id::new(2).expect("non zero"), guild_2_emojis);
cache.cache_emojis(Id::new(2), guild_2_emojis);

for id in guild_2_emoji_ids.iter().cloned() {
let global_emoji = cache.emoji(id);
assert!(global_emoji.is_some());
}

let guild_emojis = cache.guild_emojis(Id::new(2).expect("non zero"));
let guild_emojis = cache.guild_emojis(Id::new(2));

assert!(guild_emojis.is_some());
let guild_emojis = guild_emojis.unwrap();
Expand All @@ -162,11 +158,11 @@ mod tests {
fn test_emoji_removal() {
let cache = InMemoryCache::new();

let guild_id = Id::new(1).expect("non zero");
let guild_id = Id::new(1);

let emote = test::emoji(Id::new(1).expect("non zero"), None);
let emote_2 = test::emoji(Id::new(2).expect("non zero"), None);
let emote_3 = test::emoji(Id::new(3).expect("non zero"), None);
let emote = test::emoji(Id::new(1), None);
let emote_2 = test::emoji(Id::new(2), None);
let emote_3 = test::emoji(Id::new(3), None);

cache.cache_emoji(guild_id, emote.clone());
cache.cache_emoji(guild_id, emote_2.clone());
Expand All @@ -193,7 +189,7 @@ mod tests {
assert!(cache.emoji(emote.id).is_some());
assert!(cache.emoji(emote_2.id).is_none());

let emote_4 = test::emoji(Id::new(4).expect("non zero"), None);
let emote_4 = test::emoji(Id::new(4), None);

cache.update(&GuildEmojisUpdate {
emojis: vec![emote_4.clone()],
Expand Down
30 changes: 13 additions & 17 deletions cache/in-memory/src/event/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod tests {
let timestamp = Timestamp::from_str(DATETIME)?;

let channels = Vec::from([GuildChannel::Text(TextChannel {
id: Id::new(111).expect("non zero"),
id: Id::new(111),
guild_id: None,
kind: ChannelType::GuildText,
last_message_id: None,
Expand All @@ -249,7 +249,7 @@ mod tests {
})]);

let threads = Vec::from([GuildChannel::PublicThread(PublicThread {
id: Id::new(222).expect("non zero"),
id: Id::new(222),
default_auto_archive_duration: None,
guild_id: None,
kind: ChannelType::GuildPublicThread,
Expand All @@ -269,16 +269,16 @@ mod tests {
},
member: Some(ThreadMember {
flags: 0,
id: Some(Id::new(1).expect("non zero")),
id: Some(Id::new(1)),
join_timestamp: timestamp,
member: None,
presence: None,
user_id: Some(Id::new(2).expect("non zero")),
user_id: Some(Id::new(2)),
}),
})]);

let guild = Guild {
id: Id::new(123).expect("non zero"),
id: Id::new(123),
afk_channel_id: None,
afk_timeout: 300,
application_id: None,
Expand All @@ -301,7 +301,7 @@ mod tests {
name: "this is a guild".to_owned(),
nsfw_level: NSFWLevel::AgeRestricted,
owner: Some(false),
owner_id: Id::new(456).expect("non zero"),
owner_id: Id::new(456),
permissions: Some(Permissions::SEND_MESSAGES),
preferred_locale: "en-GB".to_owned(),
premium_subscription_count: Some(0),
Expand Down Expand Up @@ -329,28 +329,24 @@ mod tests {
let cache = InMemoryCache::new();
cache.cache_guild(guild);

let channel = cache
.guild_channel(Id::new(111).expect("non zero"))
.unwrap();
let channel = cache.guild_channel(Id::new(111)).unwrap();

let thread = cache
.guild_channel(Id::new(222).expect("non zero"))
.unwrap();
let thread = cache.guild_channel(Id::new(222)).unwrap();

// The channel was given to the cache without a guild ID, but because
// it's part of a guild create, the cache can automatically attach the
// guild ID to it. So now, the channel's guild ID is present with the
// correct value.
match channel.resource() {
GuildChannel::Text(ref c) => {
assert_eq!(Some(Id::new(123).expect("non zero")), c.guild_id);
assert_eq!(Some(Id::new(123)), c.guild_id);
}
_ => panic!("{:?}", channel),
}

match thread.resource() {
GuildChannel::PublicThread(ref c) => {
assert_eq!(Some(Id::new(123).expect("non zero")), c.guild_id);
assert_eq!(Some(Id::new(123)), c.guild_id);
}
_ => panic!("{:?}", channel),
}
Expand All @@ -376,7 +372,7 @@ mod tests {
explicit_content_filter: ExplicitContentFilter::None,
features: Vec::new(),
icon: None,
id: Id::new(1).expect("non zero"),
id: Id::new(1),
joined_at: None,
large: false,
max_members: None,
Expand All @@ -387,7 +383,7 @@ mod tests {
mfa_level: MfaLevel::None,
name: "test".to_owned(),
nsfw_level: NSFWLevel::Default,
owner_id: Id::new(1).expect("non zero"),
owner_id: Id::new(1),
owner: None,
permissions: None,
preferred_locale: "en_us".to_owned(),
Expand Down Expand Up @@ -431,7 +427,7 @@ mod tests {
mfa_level: guild.mfa_level,
name: "test2222".to_owned(),
nsfw_level: guild.nsfw_level,
owner_id: Id::new(2).expect("non zero"),
owner_id: Id::new(2),
owner: guild.owner,
permissions: guild.permissions,
preferred_locale: guild.preferred_locale,
Expand Down
48 changes: 22 additions & 26 deletions cache/in-memory/src/event/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ mod tests {
let cache = InMemoryCache::new();
cache.update(&InteractionCreate(Interaction::ApplicationCommand(
Box::new(ApplicationCommand {
application_id: Id::new(1).expect("non zero"),
channel_id: Id::new(2).expect("non zero"),
application_id: Id::new(1),
channel_id: Id::new(2),
data: CommandData {
id: Id::new(5).expect("non zero"),
id: Id::new(5),
name: "command name".into(),
options: Vec::new(),
resolved: Some(CommandInteractionDataResolved {
channels: HashMap::new(),
members: IntoIterator::into_iter([(
Id::new(7).expect("non zero"),
Id::new(7),
InteractionMember {
avatar: None,
communication_disabled_until: None,
Expand All @@ -108,12 +108,12 @@ mod tests {
pending: false,
permissions: Permissions::empty(),
premium_since: None,
roles: vec![Id::new(8).expect("non zero")],
roles: vec![Id::new(8)],
},
)])
.collect(),
messages: IntoIterator::into_iter([(
Id::new(4).expect("non zero"),
Id::new(4),
Message {
activity: None,
application: None,
Expand All @@ -127,7 +127,7 @@ mod tests {
discriminator: 1,
email: None,
flags: None,
id: Id::new(3).expect("non zero"),
id: Id::new(3),
locale: None,
mfa_enabled: None,
name: "test".to_owned(),
Expand All @@ -136,14 +136,14 @@ mod tests {
system: None,
verified: None,
},
channel_id: Id::new(2).expect("non zero"),
channel_id: Id::new(2),
components: Vec::new(),
content: "ping".to_owned(),
edited_timestamp: None,
embeds: Vec::new(),
flags: Some(MessageFlags::empty()),
guild_id: Some(Id::new(1).expect("non zero")),
id: Id::new(4).expect("non zero"),
guild_id: Some(Id::new(1)),
id: Id::new(4),
interaction: None,
kind: MessageType::Regular,
member: Some(PartialMember {
Expand All @@ -167,7 +167,7 @@ mod tests {
reference: None,
sticker_items: vec![MessageSticker {
format_type: StickerFormatType::Png,
id: Id::new(1).expect("non zero"),
id: Id::new(1),
name: "sticker name".to_owned(),
}],
referenced_message: None,
Expand All @@ -179,12 +179,12 @@ mod tests {
)])
.collect(),
roles: IntoIterator::into_iter([(
Id::new(8).expect("non zero"),
Id::new(8),
Role {
color: 0u32,
hoist: false,
icon: None,
id: Id::new(8).expect("non zero"),
id: Id::new(8),
managed: false,
mentionable: true,
name: "role name".into(),
Expand All @@ -196,7 +196,7 @@ mod tests {
)])
.collect(),
users: IntoIterator::into_iter([(
Id::new(7).expect("non zero"),
Id::new(7),
User {
accent_color: None,
avatar: Some("different avatar".into()),
Expand All @@ -205,7 +205,7 @@ mod tests {
discriminator: 5678,
email: None,
flags: None,
id: Id::new(7).expect("non zero"),
id: Id::new(7),
locale: None,
mfa_enabled: None,
name: "different name".into(),
Expand All @@ -218,8 +218,8 @@ mod tests {
.collect(),
}),
},
guild_id: Some(Id::new(3).expect("non zero")),
id: Id::new(4).expect("non zero"),
guild_id: Some(Id::new(3)),
id: Id::new(4),
kind: InteractionType::ApplicationCommand,
member: Some(PartialMember {
avatar: None,
Expand All @@ -239,7 +239,7 @@ mod tests {
discriminator: 1234,
email: None,
flags: None,
id: Id::new(6).expect("non zero"),
id: Id::new(6),
locale: None,
mfa_enabled: None,
name: "username".into(),
Expand All @@ -255,28 +255,24 @@ mod tests {
)));

{
let guild_members = cache.guild_members(Id::new(3).expect("non zero")).unwrap();
let guild_members = cache.guild_members(Id::new(3)).unwrap();
assert_eq!(guild_members.len(), 2);
}

{
let member = cache
.member(Id::new(3).expect("non zero"), Id::new(6).expect("non zero"))
.unwrap();
let member = cache.member(Id::new(3), Id::new(6)).unwrap();
let user = cache.user(member.user_id).unwrap();
assert_eq!(user.avatar.as_ref().unwrap(), "avatar string");
}

{
let member = cache
.member(Id::new(3).expect("non zero"), Id::new(7).expect("non zero"))
.unwrap();
let member = cache.member(Id::new(3), Id::new(7)).unwrap();
let user = cache.user(member.user_id).unwrap();
assert_eq!(user.avatar.as_ref().unwrap(), "different avatar");
}

{
let guild_roles = cache.guild_roles(Id::new(3).expect("non zero")).unwrap();
let guild_roles = cache.guild_roles(Id::new(3)).unwrap();
assert_eq!(guild_roles.len(), 1);
}
}
Expand Down
Loading