Skip to content

Commit

Permalink
Fix unused singletons in lib_guide.rs (#1539)
Browse files Browse the repository at this point in the history
* Make it one file again by instantiating a fresh Oso each time.

Co-authored-by: Graham Kaemmer <[email protected]>
Co-authored-by: Stephen Olsen <[email protected]>
Co-authored-by: Sam Scott <[email protected]>
  • Loading branch information
4 people authored Aug 4, 2022
1 parent 37ca72f commit 32f4b40
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions languages/rust/oso/examples/lib_guide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ fn types() -> anyhow::Result<()> {
is_admin: bool,
}
oso.register_class(User1::get_polar_class())?;
oso.load_str(r#"allow(actor: User1, action, resource) if actor.is_admin;"#)?;
oso.load_str(r#"allow(actor: User1, _action, _resource) if actor.is_admin;"#)?;
let user1 = User1 {
name: "alice".to_string(),
is_admin: true,
};
assert!(oso.is_allowed(user1, "foo", "bar")?);

let mut oso = Oso::new();

#[derive(Clone, PolarClass)]
struct User2 {
#[polar(attribute)]
Expand All @@ -44,9 +46,15 @@ fn types() -> anyhow::Result<()> {
.add_method("is_called_alice", User2::is_called_alice)
.build(),
)?;
oso.load_str(r#"allow(user: User2, _, _) if user.is_admin;"#)?;
oso.load_str(r#"?= allow(new User2("bob", true), "foo", "bar");"#)?;
oso.load_str(r#"?= new User2("alice", true).is_called_alice();"#)?;
oso.load_str(
r#"
allow(user: User2, _, _) if user.is_admin;
?= allow(new User2("bob", true), "foo", "bar");
?= new User2("alice", true).is_called_alice();
"#,
)?;

let mut oso = Oso::new();

#[derive(Clone, PolarClass)]
struct User3 {
Expand All @@ -56,7 +64,7 @@ fn types() -> anyhow::Result<()> {
is_admin: bool,
}
oso.register_class(User3::get_polar_class())?;
oso.load_str(r#"allow(actor, action, resource) if actor matches User3{name: "alice"};"#)?;
oso.load_str(r#"allow(actor, _action, _resource) if actor matches User3{name: "alice"};"#)?;
let user3 = User3 {
name: "alice".to_string(),
is_admin: true,
Expand All @@ -78,7 +86,9 @@ fn strings() -> anyhow::Result<()> {

oso.register_class(User::get_polar_class())?;

oso.load_str(r#"allow(actor, action, resource) if actor.username.ends_with("example.com");"#)?;
oso.load_str(
r#"allow(actor, _action, _resource) if actor.username.ends_with("example.com");"#,
)?;

let user = User {
username: "[email protected]".to_owned(),
Expand All @@ -99,7 +109,7 @@ fn vecs() -> anyhow::Result<()> {

oso.register_class(User::get_polar_class()).unwrap();

oso.load_str(r#"allow(actor, action, resource) if "HR" in actor.groups;"#)?;
oso.load_str(r#"allow(actor, _action, _resource) if "HR" in actor.groups;"#)?;

let user = User {
groups: vec!["HR".to_string(), "payroll".to_string()],
Expand All @@ -118,7 +128,7 @@ fn maps() -> anyhow::Result<()> {
}

oso.register_class(User::get_polar_class())?;
oso.load_str(r#"allow(actor, action, resource) if actor.roles.project1 = "admin";"#)?;
oso.load_str(r#"allow(actor, _action, _resource) if actor.roles.project1 = "admin";"#)?;

let user = User {
roles: maplit::hashmap! { "project1".to_string() => "admin".to_string() },
Expand All @@ -144,7 +154,7 @@ fn enums() -> anyhow::Result<()> {
.add_method("is_admin", |u: &UserType| matches!(u, UserType::Admin))
.build(),
)?;
oso.load_str(r#"allow(actor, action, resource) if actor.is_admin();"#)?;
oso.load_str(r#"allow(actor, _action, _resource) if actor.is_admin();"#)?;

let user = UserType::Admin;
assert!(oso.is_allowed(user, "foo", "bar")?);
Expand All @@ -168,7 +178,7 @@ fn iters() -> anyhow::Result<()> {
)
.unwrap();

oso.load_str(r#"allow(actor, action, resource) if "payroll" in actor.get_group();"#)?;
oso.load_str(r#"allow(actor, _action, _resource) if "payroll" in actor.get_group();"#)?;

let user = User {
groups: vec!["HR".to_string(), "payroll".to_string()],
Expand Down

1 comment on commit 32f4b40

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 32f4b40 Previous: 37ca72f Ratio
rust_get_attribute 42194 ns/iter (± 1228) 58396 ns/iter (± 3734) 0.72
n_plus_one/100 2240461 ns/iter (± 12330) 2781513 ns/iter (± 77046) 0.81
n_plus_one/500 10850411 ns/iter (± 127283) 13212312 ns/iter (± 236317) 0.82
n_plus_one/1000 21556109 ns/iter (± 77247) 26238425 ns/iter (± 618856) 0.82
unify_once 880 ns/iter (± 15) 1151 ns/iter (± 2487) 0.76
unify_twice 2498 ns/iter (± 47) 3102 ns/iter (± 109) 0.81
many_rules 61182 ns/iter (± 1742) 78951 ns/iter (± 2819) 0.77
fib/5 526624 ns/iter (± 8979) 666680 ns/iter (± 18203) 0.79
prime/3 17919 ns/iter (± 658) 22699 ns/iter (± 1255) 0.79
prime/23 17920 ns/iter (± 637) 22619 ns/iter (± 1449) 0.79
prime/43 17905 ns/iter (± 706) 22722 ns/iter (± 1731) 0.79
prime/83 17926 ns/iter (± 618) 22476 ns/iter (± 1608) 0.80
prime/255 16633 ns/iter (± 539) 20820 ns/iter (± 1760) 0.80
indexed/100 5802 ns/iter (± 493) 7706 ns/iter (± 2750) 0.75
indexed/500 6940 ns/iter (± 1457) 9317 ns/iter (± 2386) 0.74
indexed/1000 8972 ns/iter (± 788) 12225 ns/iter (± 2102) 0.73
indexed/10000 21300 ns/iter (± 1666) 20050 ns/iter (± 4828) 1.06
not 5862 ns/iter (± 78) 7445 ns/iter (± 670) 0.79
double_not 12278 ns/iter (± 180) 15459 ns/iter (± 457) 0.79
De_Morgan_not 7989 ns/iter (± 153) 10065 ns/iter (± 484) 0.79
load_policy 895456 ns/iter (± 4906) 1166159 ns/iter (± 43904) 0.77
partial_and/1 30774 ns/iter (± 1104) 39746 ns/iter (± 2344) 0.77
partial_and/5 106240 ns/iter (± 2743) 140739 ns/iter (± 5589) 0.75
partial_and/10 203608 ns/iter (± 4489) 266548 ns/iter (± 9194) 0.76
partial_and/20 422389 ns/iter (± 9055) 539432 ns/iter (± 20766) 0.78
partial_and/40 906328 ns/iter (± 13709) 1149816 ns/iter (± 43658) 0.79
partial_and/80 2078490 ns/iter (± 17526) 2672283 ns/iter (± 45679) 0.78
partial_and/100 2746743 ns/iter (± 19426) 3570325 ns/iter (± 71524) 0.77
partial_rule_depth/1 95816 ns/iter (± 3364) 126976 ns/iter (± 5832) 0.75
partial_rule_depth/5 323935 ns/iter (± 6681) 421756 ns/iter (± 22627) 0.77
partial_rule_depth/10 725084 ns/iter (± 12744) 934869 ns/iter (± 24928) 0.78
partial_rule_depth/20 2046512 ns/iter (± 14107) 2707712 ns/iter (± 49529) 0.76
partial_rule_depth/40 7477115 ns/iter (± 116327) 10418720 ns/iter (± 180859) 0.72
partial_rule_depth/80 39717880 ns/iter (± 227135) 63523848 ns/iter (± 3464896) 0.63
partial_rule_depth/100 71785680 ns/iter (± 350772) 121457587 ns/iter (± 2442505) 0.59

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.