Skip to content

Add documentation and examples for multiple section types (p2, r2, m2, e2)#405

Closed
Copilot wants to merge 3 commits intomasterfrom
copilot/add-rust-multiple-section-types
Closed

Add documentation and examples for multiple section types (p2, r2, m2, e2)#405
Copilot wants to merge 3 commits intomasterfrom
copilot/add-rust-multiple-section-types

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 8, 2025

Casbin-rs already supports multiple section types via EnforceContext and add_named_policy, but this wasn't documented, making the feature invisible to users looking for parity with Go/Node.js/Java implementations.

Changes

  • README.md: Added "Multiple Section Types" section with model configuration and usage examples
  • README.md: Added entry to examples table linking to existing multi_section_model.conf and multi_section_policy.csv
  • src/enforcer.rs: Added test_multi_section_model covering section enforcement and policy management

Usage

use casbin::prelude::*;
use casbin::EnforceContext;

let e = Enforcer::new("examples/multi_section_model.conf", "examples/multi_section_policy.csv").await?;

// Default section (p, r, m, e)
e.enforce(("alice", "read", "project1"))?;

// Second section (p2, r2, m2, e2)
e.enforce_with_context(EnforceContext::new("2"), ("james", "execute"))?;

Model definition supports multiple sections:

[request_definition]
r = sub, act, obj
r2 = sub, act

[policy_definition]
p = sub, act, obj
p2 = sub, act

[matchers]
m = r.sub == p.sub && r.act == p.act && r.obj == p.obj
m2 = r2.sub == p2.sub && r2.act == p2.act
Original prompt

This section details on the original issue you should resolve

<issue_title>rust multiple section types example</issue_title>
<issue_description>I found multiple section types example with go、node 、java. but can't find rust, does rust support multiple section type?
https://casbin.org/docs/syntax-for-models#multiple-section-types</issue_description>

Comments on the Issue (you are @copilot in this section)

@hsluoyz need to support `p2` like Golang Casbin

Like Node-Casbin's issue: apache/casbin-node-casbin#472

refer to PR: apache/casbin-node-casbin#518</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review December 8, 2025 17:37
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 8, 2025

Benchmark for 3c53722

Click to view benchmark
Test PR Benchmark Master Benchmark %
b_benchmark_rbac_model_large 9.7±0.05ms 9.6±0.04ms +1.04%
benchmark priority model 1390.9±20.89ns 1364.0±23.64ns +1.97%
benchmark_abac_model 1108.6±16.09ns 1104.6±10.13ns +0.36%
benchmark_basic_model 1200.4±39.95ns 1159.7±11.25ns +3.51%
benchmark_key_match 4.0±0.08µs 4.0±0.07µs 0.00%
benchmark_raw 0.0±0.00ns 0.0±0.00ns NaN%
benchmark_rbac_model 3.0±0.05µs 3.2±0.09µs -6.25%
benchmark_rbac_model_medium 794.9±5.06µs 793.0±5.13µs +0.24%
benchmark_rbac_model_with_domains 1963.4±31.34ns 1967.9±39.91ns -0.23%
benchmark_rbac_with_deny 5.3±0.09µs 5.2±0.08µs +1.92%
benchmark_rbac_with_resource_roles 1373.7±21.35ns 1422.2±20.88ns -3.41%
benchmark_role_manager_large 4.3±0.03ms 4.3±0.01ms 0.00%
benchmark_role_manager_medium 268.0±4.94µs 268.5±2.11µs -0.19%
benchmark_role_manager_small 85.4±1.31µs 83.8±1.23µs +1.91%

Copilot AI and others added 2 commits December 8, 2025 17:48
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
…torials

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for multiple section types in Rust Add documentation and examples for multiple section types (p2, r2, m2, e2) Dec 8, 2025
Copilot AI requested a review from hsluoyz December 8, 2025 17:56
@hsluoyz hsluoyz closed this Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust multiple section types example

3 participants